Jump to content

Finding Parent Node.


Scrier
 Share

Recommended Posts

Im looking to do a match template that I can loop up in until I find a specific node. The template I have atm looks like this:

<xsl:template match="if|and|case|condition|sequence" mode="repetition">	<xsl:choose>		<xsl:when test="sequence">			<!-- Do something else, we found the node -->		</xsl:when>		<xsl:otherwise>			<xsl:apply-templates select="parent::*" mode="repetition" />		</xsl:otherwise>	</xsl:choose></xsl:template>

And as you probably understand the xml is nested if, and, case, condition parameters that i need to go up through to find the overlying sequence, here is an example of the tree:

<sequence>	<something /></sequence><validation>	<case>		<if>			<and />			<and />		</if>	</case></validation>

so I can be in any underlying node on if, and etc and need to find up to the sequence nodeThe current template complains that I try to loop around ".", although I have tried parent::node() and some others but all seems to think I loop around the same element and not the overlying.

Link to comment
Share on other sites

Based on the very limited samples you gave us, it looks like you're working too hard to get what you're after. I inferred that you are trying to find the sequence node that has a sibling validation node. Based on your example all you need to do is look for the preceding sibling. The trick here is to remember that preceding sibling is done in reverse document order, so you need to index it, otherwise you'll get the value for the first one, not the one you are looking for. All that said, here is my examples. I modified your XML into what I think you have

		<data>			<sequence>				<something>if1</something>			</sequence>			<validation>				<case>					<if>						<and />						<and />					</if>				</case>			</validation>			<sequence>				<something>if2</something>			</sequence>			<validation>				<case>					<if>						<and />						<and />					</if>				</case>			</validation>		</data>

XSLT

		<xsl:stylesheet			xmlns:xsl="http://www.w3.org/1999/XSL/Transform"			xmlns:msxsl="urn:schemas-microsoft-com:xslt"			version="1.0">			<xsl:output method="html" />			<xsl:template match="/">				<xsl:for-each select="//validation">					<xsl:value-of select="preceding-sibling::sequence[1]/something"/>					<br/>				</xsl:for-each>			</xsl:template>		</xsl:stylesheet>

result

if1if2

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...