我有一个这样的 XML 文档:
<Module Name="DacInPlaceUpgradeLtmTest" Desc="" >
<TestCase Name="ExecuteInPlaceUpgradeTest">
<TestCase Name="BugRepro">
<TestCase Name="295130">
<Variation Id="1" Desc="Sql - EmptyAlterScript">
<Variation Id="2" Desc="Sql - EmptyDatabase">
</TestCase>
</TestCase>
</TestCase>
</Module>
我使用 xsl 来获取一个值:
ExectionInplaceUPgradeTest BugRepro 295130
通过使用以下模板:
<xsl:template match="TestCase//Variation">
<xsl:for-each select="..@Name">
今天只能拿到295130,不知道怎么才能拿到TestCase父节点的所有属性。
最佳答案
I wonder how can I get all Attributes of the parent nodes which is TestCase
您可能需要 XPath
ancestor-or-self
轴:ancestor-or-self::TestCase/@Name
how to concat the attribte names of the parent nodes
这实际上取决于模板上下文和您已经在处理的代码。以您的片段作为引用,我会写得更好:
<xsl:template match="TestCase[Variation]">
<xsl:for-each select="ancestor-or-self::TestCase/@Name">
<xsl:value-of select="concat(.,' ')"/>
</xsl:for-each>
</xsl:template>
这将打印从
TestCase
开始的想要的字符串具有 Variation
的节点作为 child 的节点。
关于xslt - 如何连接父节点的属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6643816/