Search code examples
xsltxpathattributespositionnodes

Position of node with attribute


How can i select the position of a node with a specific attribute in XSL.

XML:

<document type="async" page="tabTabel">
 <tabs>
  <table>
   <row type="header">
    <column type="text">href</column>
    <column type="number">Mapnr.</column>
    <column type="mapposition">Nr.</column>
    <column type="text">Description</column>
    <column type="text">Document</column>
    <column type="date">Date</column>
   </row>

   <row type="data">
    <column><![CDATA[]]></column>
    <column><![CDATA[10]]></column>
    <column><![CDATA[17]]></column>
    <column><![CDATA[Documentation may 2013 .pdf]]></column>
    <column><![CDATA[Documentation may 2013 .pdf]]></column>
    <column><![CDATA[03-04-2014]]></column>
   </row>

  </table>
 </tabs>
</document>

Current not-working XSLT:

<xsl:template match="tabs//row[@type='data']>
 <xsl:variable name="mapnumber">
  <xsl:value-of select="../row[@type='header']/column[@type='mapposition'][position()]" />
 </xsl:variable>
</xsl:template>

I want the index number/position of the column with type 'mapposition'. How can I do this?


Solution

  • Try:

    <xsl:variable name="mapnumber" select="count(../row[@type='header']/column[@type='mapposition']/preceding-sibling::column) + 1" />
    

    In view of your edit, you'd probably want to do something like:

    <xsl:template match="table">
        <xsl:variable name="mapnumber" select="count(row[@type='header']/column[@type='mapposition']/preceding-sibling::column) + 1" />
        <xsl:value-of select="row[@type='data']/column[$mapnumber]" />
    </xsl:template>