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

4

我有一个简单的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个回答

2

"我正在尝试复制每个包含子元素的元素,其中@attribute-id为“exportdate”,并且具有特定文本值。"

空模板用于删除元素。因此,您的XSL当前应删除'exportdate'等于'2015-09-19'的custom-object,并复制其他元素。如果您想要相反的结果,请尝试使用具有相反意义的XPath,例如:

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

谢谢,你是对的。另一个简短的问题:如何匹配文本值以'2015-'开头/包含的子项?有没有办法使用正则表达式? - A. Lutz
在XSLT 1.0中,您可以使用starts-with()contains()函数。 - har07

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接