xml - How to pick all the firsts occurrences of data when iterating through xsl:for-each -


in xslt want iterate through 1 node of each country. dont know how many countries in xml...

with file:

<catalog>     <cd>         <title>empire burlesque</title>                     <country>usa</country>     </cd>     <cd>         <title>hide heart</title>         <country>uk</country>     </cd>     <cd>         <title>greatest hits</title>         <country>usa</country>     </cd>     <cd>         <title>greatest hits</title>         <country>uk</country>     </cd>      <cd>         <title>greatest hits</title>         <country>fin</country>     </cd>     <cd>         <title>super greatest hits</title>         <country>spa</country>     </cd>     <cd>         <title>greatest hits2</title>         <country>fin</country>     </cd>  </catalog> 

i need iterate first node of usa, uk, fin , spa. want iterate through first of each country, , forget addional nodes.

with like:

<xsl:for-each select="catalog/cd[country='usa']">   <xsl:if test="position() = 1"> 

i have know name of each country in xslt... not work me...

my best solution problem using keys , operator | of xpath.

first of create key through field wanna iterate:

<xsl:key name="countries" match="catalog/cd" use="country" /> 

then iterate cd, 1 special condition:

<xsl:for-each select="catalog/cd[count(.| key('countries', country)[1])=1 ]"> 

operator | union operator in xpath.
union of 1 node himself results in single node, not 2. important solution.

.|key('countries', country)[1] means if current node iterating union first accurrence of country in document same node, want iterate through it. else dont!


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -