This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Not a nested for loop.


Hi Dave,

> I need to loop through each of the main input document records (r
> elements) and if r/cat-num is any one of the values of the external
> file, /bk/n then I need to copy the r element and contents through
> to the output.
>
> Can xslt2.0 help with this form of filtering?

Of course you can it currently with:

<xsl:copy-of select="r[cat-num = document('external.xml')/bk/n]" />

But it's not particularly efficient.

For something more efficient you could use a key. In XSLT 1.0 you
would do:

<xsl:key name="nums" match="bk/n" use="." />

<xsl:for-each select="r">
  <xsl:variable name="r" select="." />
  <xsl:for-each select="document('external.xml')">
    <xsl:if test="key('nums', $r/cat-num)">
      <xsl:copy-of select="$r" />
    </xsl:if>
  </xsl:for-each>
</xsl:for-each>

In XSLT 2.0 you can do:

<xsl:key name="nums" match="bk/n" use="." />

<xsl:for-each select="r">
  <xsl:if test="document('external.xml')
                  /key('nums', current()/cat-num)">
    <xsl:copy-of select="." />
  </xsl:if>
</xsl:for-each>

Or you could do:

<xsl:key name="nums" match="bk/n" use="." />

<xsl:copy-of select="for $r in r
                     return if (document('external.xml')
                                  /key('nums', $r/cat-num))
                            then $r
                            else ()" />

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]