使用 xslt 复制包含特定子元素的父元素 [英] Copy parent element that contains specific child element using xslt
问题描述
我有一个简单的 xml:
I have a simple xml:
<custom-objects>
<custom-object id="1">
<object-attribute attribute-id="address1">Warndtstr. 33</object-attribute>
<object-attribute attribute-id="branch">01</object-attribute>
<object-attribute attribute-id="catalogid">7991</object-attribute>
<object-attribute attribute-id="exportdate">2015-09-19</object-attribute>
</custom-object>
<custom-object>
...
</custom-object>
</custom-objects>
我试图简单地复制每个 <custom-object>
元素,其中包含一个子元素,其中 @attribute-id
是 "exportdate"
并具有特定的文本值.
I'm trying to simply copy every <custom-object>
element which contains a child where @attribute-id
is "exportdate"
and has a specific textvalue.
这是我的 xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="custom-object[object-attribute[@attribute-id='exportdate']='2015-09-19']"/>
</xsl:stylesheet>
匹配在用作 xpath 时有效.xslt 返回空结果.
The match is working when using as xpath. The xslt returns an empty result.
- 为什么在这种情况下不起作用?
- 我的错误在哪里?
推荐答案
我试图简单地复制每个包含子元素的元素,其中@attribute-id 是exportdate"并且具有特定的文本值."
空模板应该用于删除元素.所以目前,您的 XSL 应该删除 custom-object
其中 'exportdate' 等于 '2015-09-19',并复制其他元素.如果您想要相反的含义,请尝试使用具有相反含义的 XPath,例如:
Empty template supposed to be used for deleting elements. So currently, your XSL should remove custom-object
where 'exportdate' equals '2015-09-19', and copy other elements. If you want the opposite, try using XPath with the opposite meaning, for example :
<xsl:template
match="custom-object[not(object-attribute[@attribute-id='exportdate']='2015-09-19')]"/>
这篇关于使用 xslt 复制包含特定子元素的父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!