html - How to get the position of a node based on the value of a child element -
in xml want specific menuitems/menuitem
nodes @ different arbitrary positions under parent (i don't want hardcoded position selector).
is possible position of menuitem
node has right value in name element under it, meaning menuitems/menuitem/name
. in short: selecting menuitem
has right name
value under it.
<one> <menuitems> <menuitem> <!-- dont want 1 --> <name> ... </name> </menuitem> <menuitem> <!-- want 1 @ position 2 under <one> --> <name> ... <!-- based 1 correct name value here --> </name> </menuitem> </menuitems> </one> <two> <menuitems> <menuitem> <!-- want 1 @ position 1 under <two> --> <name> ... </name> </menuitem> </menuitems> </two>
i can find out if 1 menuitem
under menuitems
has correct name
value. so:
<xsl:value-of select="current()/menuitems/menuitem/name = 'ohyes'"></xsl:value-of>
which return true
. @ position menuitem
amongs other menuitem
returned true
? selecting under same parent , @ same level.
i want avoid this:
<xsl:if test="current()/menuitems/menuitem[1]/name = 'ohyes'"> .. </xsl:if> <xsl:if test="current()/menuitems/menuitem[2]/name = 'ohyes'"> .. </xsl:if>
use this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:for-each select="/*/*/menuitems/menuitem[name='ohyes']"> position: <xsl:text/> <xsl:value-of select="count(preceding-sibling::menuitem) +1"/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
**when transformation applied on following xml document: ... --> ohyes --> ohyes
the wanted, correct result produced:
position: 2 position: 1
Comments
Post a Comment