Reputation: 2237
How to access the child node of parent 1 from child node of parent 2 in XSLT?
<Test>
<SOLUTIONS>
<SOLUTION>
<OBSERVATIONS>
<OBSERVATION Key = "1ASED">A1</OBSERVATION>
<OBSERVATION Key = "2DFED">A2</OBSERVATION>
<OBSERVATION Key = "3DGEE">A3</OBSERVATION>
</OBSERVATIONS>
<ITRs>
<ITR Key = "ASE1">P1</ITR>
<ITR Key = "GGEE1">P2</ITR>
<ITR Key = "GERFECE1">P3</ITR>
</ITRs>
</SOLUTION>
<SOLUTION>
<OBSERVATIONS>
<OBSERVATION Key = "ABCD">A1</OBSERVATION>
<OBSERVATION Key = "EFGH">A2</OBSERVATION>
<OBSERVATION Key = "IJKL">A3</OBSERVATION>
</OBSERVATIONS>
...
</SOLUTION>
</SOLUTIONS>
</Test>
For each ITR, I need the observation to be printed. For First ITR inside , I have to link the Key of this to Key of the Observation. So, total 9 records to be displayed.
My XSLT is
<xsl:for-each select="/Test/SOLUTIONS/SOLUTION">
<xsl:for-each select="ITRs/ITR">
<xsl:variable name="Key_ITRPS" select="@Key"/>
<xsl:for-each select="/Test/SOLUTIONS/SOLUTION/OBSERVATIONS/OBSERVATION">
<xsl:variable name="srcKey_A" select="@Key"/>
<xsl:element name="Relationship">
<xsl:attribute name="RelCommonKey">
<xsl:value-of select="concat($Key_ITRPS,$srcKey_A)"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
The output I require is
RelCommonKey
ASE11ASED
ASE12DFED
ASE13DGEE
GGEE11ASED
GGEE12DFED
GGEE13DGEE
GERFECE11ASED
GERFECE12DFED
GERFECE13DGEE
The problem i am facing here is along with the above data, i am evening noticing the Observation Key in the second too.
the ITR key "ASE1" is also linking to "ABCD, "EFGH", "IJKL" also. Please help me in restricting the data as displayed above.
Upvotes: 2
Views: 1488
Reputation: 70142
your problem is that your XPath queries are all 'anchored' on the root node, "/Test", what you need to do instead is use the current context and the 'parent' XPath axis to navigate fron the ITR element to the OBSERVATIONS within the same SOLUTION. The following XSLT gives the output you require:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates select="//SOLUTION"/>
</xsl:template>
<xsl:template match="SOLUTION">
<!-- iterat over the ITRs for this solution -->
<xsl:for-each select="ITRs/ITR">
<xsl:variable name="Key_ITRPS" select="@Key"/>
<!-- iterate over the observations -->
<xsl:for-each select="../../OBSERVATIONS/OBSERVATION">
<!-- the ITR Key -->
<xsl:value-of select="$Key_ITRPS"/>
<!-- the related observation -->
<xsl:value-of select="@Key"/>
<!-- a newline -->
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2