xsl-list
[Top] [All Lists]

Re: Need help on that tricky selection

2004-05-25 12:48:03
Hi Christoph,

what i want to do is: select all the <ort> elements, complete
with all their childs, where the <hst> is equal to the <ort_nr>
and throw the rest away.

one problem is, that i need exactly the order which is given
within the <hst_list> (the index attribute)
the other problem may be the performance, because it's possible
that this xml contains  about 3-5 thousend of <ort> elements

Use a key. This will make the code easy and it will make it as
efficient as it's possible to be. Index the <ort> elements by the
value of their <ort_nr> child:

<xsl:key name="orte" match="ort" use="ort_nr" />

Then you can get the <ort> element with the <ort_nr> 1031501 using:

  key('orte', '1031501')

and if you're currently on a particular <hst> element, you can get the
one corresponding to this <hst> element with:

  key('orte', .)

So to process the <ort> elements in the order specified by the
<hst_list>, you'd do something like:

<xsl:template match="lines">
  <orte>
    <xsl:for-each select="hst_list/hst">
      <xsl:copy-of select="key('orte', .)" />
    </xsl:for-each>
  </orte>
</xsl:template>

The only <ort> elements that will be copied are those selected through
the key, so only those corresponding to an <hst> element. Of course
you could use an <xsl:apply-templates> instead of an <xsl:copy-of> if
you want to process them rather than just get a copy of them.

Oh, and if the <hst> elements might appear in a different order from
that specified by their index attributes, then sort them with:

  <xsl:for-each select="hst_list/hst">
    <xsl:sort select="@index" data-type="number" />
    <xsl:copy-of select="key('orte', .)" />
  </xsl:for-each>

Cheers,

Jeni

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



<Prev in Thread] Current Thread [Next in Thread>