xml 在 XSLT 中获取父元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12685970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting parent element in XSLT
提问by VextoR
I have an XML like this:
我有一个这样的 XML:
<PurchaseOrder>
<ID>1</ID>
<PurchaseOrderLine>
<DATA>100<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>200<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>300<DATA>
</PurchaseOrderLine>
</PurchaseOrder>
<PurchaseOrder>
<ID>2</ID>
<PurchaseOrderLine>
<DATA>100<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>200<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>300<DATA>
</PurchaseOrderLine>
</PurchaseOrder>
<PurchaseOrder>
<ID>3</ID>
<PurchaseOrderLine>
<DATA>100<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>200<DATA>
</PurchaseOrderLine>
<PurchaseOrderLine>
<DATA>300<DATA>
</PurchaseOrderLine>
</PurchaseOrder>
and XSL:
和 XSL:
<xsl:template match="PurchaseOrder">
<xsl:apply-templates select="PurchaseOrderLine"/>
</xsl:template>
<xsl:template match="PurchaseOrderLine">
<!-- I want to get the PurchaseOrder\ID here for the current PurchaseOrder -->
</xsl:template>
How can I get current parent element value (PurchaseOrder\ID) in PurchaseOrderLine?
如何在 PurchaseOrderLine 中获取当前父元素值 (PurchaseOrder\ID)?
采纳答案by Tomalak
Seems like you have skipped some basic reading on XPath.
似乎您跳过了一些关于 XPath 的基本阅读。
<xsl:template match="PurchaseOrderLine">
<xsl:value-of select="../ID" />
</xsl:template>
回答by rainabba
If you want your templates to be atomic (isolated and reusable), you should be referencing a parent node this way. Instead, when calling the template, pass in the reference you want to be able to use. This way you could use this template for the same type of node, even if it has a different context/parent (so long as you can still load the parameter).
如果你希望你的模板是原子的(隔离的和可重用的),你应该以这种方式引用父节点。相反,在调用模板时,传入您希望能够使用的引用。这样您就可以将此模板用于相同类型的节点,即使它具有不同的上下文/父节点(只要您仍然可以加载参数)。
<xsl:template match="PurchaseOrder">
<xsl:apply-templates select="PurchaseOrderLine">
<xsl:with-param name="PurchaseOrder" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="PurchaseOrderLine">
<xsl:param name="PurchaseOrder"/>
<!-- I want to get the PurchaseOrder\ID here for the current PurchaseOrder -->
</xsl:template>
Now in your PurchaseOrderLine template, you can reference the $PurchaseOrder variable.
现在,在您的 PurchaseOrderLine 模板中,您可以引用 $PurchaseOrder 变量。
回答by Joates
Not sure if this is where you are going but you can match the Parent node by doing the following, this checks if the parent node has the Child node.
不确定这是否是您要去的地方,但您可以通过执行以下操作来匹配父节点,这将检查父节点是否具有子节点。
<xsl:template match="//*[PurchaseOrderLine]>
<!--- Do you stuff here with parent context--->
</xsl:template>
With this you can do several things, you can select a PurchaseOrderLine with an ID and Data value.
有了它,您可以做几件事,您可以选择带有 ID 和数据值的 PurchaseOrderLine。
<xsl:template match="//PurchaseOrder[ID=3 and PurchaseOrderLine/DATA=100]">
<!--- Do stuff with parent that has the ID of 3 And the DATA of 200 --->
</xsl:template>

