Using
//page[@id=$yourId]/node()[not(self::page)]
This selects all nodes that are not page and are children of any page in the document whose string value has the id attribute equal to the line contained in $yourId (most likely, you replace $yourId above with the specific desired line, for example '1' )
Here is a simple XSLT-based check :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="pId" select="3"/> <xsl:template match="/"> <xsl:copy-of select="//page[@id=$pId]/node()[not(self::page)]"/> </xsl:template> </xsl:stylesheet>
when this conversion is applied to the provided XML document (wrapped in one top node to make it correct):
<pages> <page id='1'> <title>Page 1</title> <page id='2'> <title>Sub Page 1</title> </page> <page id='3'> <title>Sub Page 2</title> </page> </page> <page id='4'> <title>Page 2</title> </page> </pages>
required, the correct result is obtained :
<title>Sub Page 2</title>
Pay attention . It has been suggested that the id value uniquely identifies a page . If this is not the case, the proposed XPath expression will select all page elements whose id attribute has the value of the string $yourId .
If this is the case, and only one page element should be selected, the OP should indicate which of the many page elements with this id should be selected.
For example, this may be the first :
(//page[@id=$yourId]/node()[not(self::page)])[1]
or last :
(//page[@id=$yourId]/node()[not(self::page)])[last()]
or...
Dimitre novatchev
source share