, , . , . , XPath .
XML
<fruits>
<name>apple</name>
<name>apple</name>
<name>apple</name>
<name>apple</name>
<name>apple</name>
<name>orange</name>
<name>apple</name>
<name>apple</name>
<name>apple</name>
</fruits>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:param name="search-string" select="'orange'"/>
<xsl:template match="/">
<xsl:apply-templates select="//*[contains(text(),$search-string)]"/>
</xsl:template>
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name())"/>
<xsl:if test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
<xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
/fruits/name[6]
If the search bar was "apple", it will return the path for each item namewith an "apple" in it. If you do not need all these paths, you can return either the first or the last by changing xsl:apply-templates...
First
<xsl:apply-templates select="(//*[contains(text(),$search-string)])[1]"/>
Last
<xsl:apply-templates select="(//*[contains(text(),$search-string)])[last()]"/>
source
share