使用Xpath的XSL返回树以获取属性

时间:2014-02-10 09:42:12

标签: xml xslt xpath

陷入困境......

已经删除了一些东西以使其变小,因为我怀疑你需要看到150,000行xml。

<Report>
<item id="1">
<vsection>
<hsection>
<component id="26">
<stuff1></stuff1>
<stuff2></stuff2>
<stuff3></stuff3>
</component>
</hsection>
</vsection>
</item>
</Report>

显然,XML中有更多内容,但我突出了我在第1项的第26个节点的关键点。我已经通过我的XSLT(v1.0)搜索到了那一点,现​​在我需要重新启动并确定哪个项目[@id]是我所在的组件。

非常短暂的当前XSL :(我知道的那种情况差异)

<xsl:template match="/">
<xsl:apply-templates select="report/item" />
</xsl:template>

<xsl:template match="Report/Item">
<xsl:if test="Properties/DrwQty &gt; 1">
    <xsl:apply-templates select="VSection/HSection/Component" mode="drawers"/>
</xsl:if>
</xsl:template>

<xsl:template match="VSection/HSection/Component">
<xsl:variable name="DisplayName" select="DisplayName" />
<xsl:if test="IsPresent[text()='Yes'] and contains($DisplayName, 'Order')">
    <ID><xsl:value-of select="../../../*[@id]"/></ID>
</xsl:if>
</xsl:template>

“../../../* [@ id]”一直向我回复所有节点的所有答案。我只需要获得Item节点属性的单一答案,任何人都能帮我解释一下这个问题吗? 如果我替换为xsl:value-of select =“@ id”我得到答案“26”,这是组件节点的当前属性:(

干杯, 阿利斯泰尔。

2 个答案:

答案 0 :(得分:3)

尝试:

<ID><xsl:value-of select="../../../@id"/></ID>

或:

<ID><xsl:value-of select="ancestor::Item/@id"/></ID>

注意:
1. XML区分大小写;你不能通过改变案例来“简化”你的输入 方括号是用于表示谓词的符号。

答案 1 :(得分:-1)

如果你得到的是所有id而不是1,你应该使用谓词:

**<ID><xsl:value-of select="ancestor::item[1]/@id"/></ID>**