使用xslt复制包含特定子元素的父元素

时间:2016-05-10 07:36:56

标签: xml xslt xpath

我有一个简单的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"元素。

这是我的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返回一个空结果。

  • 为什么这种情况不起作用?
  • 我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

  

&#34;我试图简单地复制包含子项的每个元素,其中@ attribute-id为&#34; exportdate&#34;并具有特定的文本值。&#34;

空模板应该用于删除元素。目前,您的XSL应该移除custom-object其中&#39; exportdate&#39;等于&#39; 2015-09-19&#39;,并复制其他元素。如果您想要相反,请尝试使用具有相反含义的XPath,例如:

<xsl:template 
    match="custom-object[not(object-attribute[@attribute-id='exportdate']='2015-09-19')]"/>
相关问题