如何在XSLT中从父节点2的子节点访问父节点1的子节点?
<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>
对于每个ITR,我需要打印观察。对于内部的第一个ITR,我必须将它的Key链接到观察的Key。因此,共显示9条记录。
我的XSLT
<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>
我需要的输出是
RelCommonKey
ASE11ASED
ASE12DFED
ASE13DGEE
GGEE11ASED
GGEE12DFED
GGEE13DGEE
GERFECE11ASED
GERFECE12DFED
GERFECE13DGEE
我在这里遇到的问题是上面的数据,我晚上也注意到第二个观察键。
ITR键“ASE1”也链接到“ABCD”,“EFGH”,“IJKL”。请帮我限制上面显示的数据。
答案 0 :(得分:2)
你的问题是你的XPath查询全部“锚定”在根节点“/ Test”上,你需要做的是使用当前上下文和'父'XPath轴来导航ITR元素到同一解决方案中的观察。以下XSLT提供了您需要的输出:
<?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>