Here is some example XML from which I only need to select the closest ancestor to an element that contains a given string, the ancestor must have the @isDoc
attribute.
In my below example, I would expect to only get the <example>
node in the resulting nodeset if the value of $macroAlias
was match
.
<container isDoc="">
<site isDoc="">
<home isDoc="">
<page>Some text</page>
<page>Some text</page>
<example isDoc="">
<title>A title</title>
<body><![CDATA[Some text and a page containing a <p>string to match</p>]]></body>
</example>
<page>Some text</page>
</home>
</site>
</container>
My current query can be found below. The problem with it is that it selects not only the closest ancestor but all other ancestors above that too. I really only want the ancestor node (with the @isDoc
attribute) of the node containing the given string ($macroAlias
).
<xsl:if test="$macroAlias != ''">
<xsl:variable name="nodes"
select="//node()[contains(., $macroAlias)][ancestor::*[@isDoc][1]]/parent::*[@isDoc]"/>
<ul>
<li>
<xsl:for-each select="$nodes">
<xsl:value-of select="name()"/>
</xsl:for-each>
</li>
</ul>
</xsl:if>
I've tried many different ways to achieve this and either end up with the same result or no results in my output.