XPath regarding location variable in source document

When repeating through nodes in a node-set variable, I want an XPATH 1.0 expression that returns all the ancestors of a node (for example, from $ myVariable [7]) - not ancestors in node -set, but ancestors in the source document.

I thought one of them would work, but did not.

select="//*[generate-id()=generate-id($myVariable[7])]/ancestor::*" select="id(generate-id($myVariable[7]))/ancestor::*" 

I close?

Edit: This has nothing to do with my question, but I had //ancestor ; that an extra slash is not needed.

+4
source share
1 answer

Your expression :

 //*[generate-id()=generate-id($myVariable[7])]/ancestor::* 

right one .

The reason that it "does not work" may be due to the fact that $myVariable[7] does not contain the expected.

Here is a simple complete example using the above expression and getting the expected. correct results :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:variable name="myVariable" select="/*/*/*/*/num"/> <xsl:template match="/"> <xsl:for-each select= "//*[generate-id() = generate-id($myVariable[7]) ] /ancestor::* "> <xsl:value-of select="name()"/> <xsl:text>&#xA;</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the following XML document :

 <a> <b> <c> <nums> <num>01</num> <num>02</num> <num>03</num> <num>04</num> <num>05</num> <num>06</num> <num>07</num> <num>08</num> <num>09</num> <num>10</num> </nums> </c> </b> </a> 

the desired, correct result is created (the names of all the ancestors of $myVariable[7] ) :

 a b c nums 
+2
source

Source: https://habr.com/ru/post/1396063/


All Articles