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

标签 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>包含 child 的元素 @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 返回空结果。

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

最佳答案

"I'm trying to simply copy every element which contains a child where @attribute-id is "exportdate" and has a specific textvalue."

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

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

关于xml - 使用 xslt 复制包含特定子元素的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132078/

相关文章:

c# - 在 C# 中根据 XSD 模式验证 json 数据

html - XSLT 转换从混合内容中移除 HTML 元素

android - 在 RelativeLayout 中按中心线对齐 View

xml - 验证XPath表达式

xml - Cocoon lite/XML 和 XSLT 发布框架

xml - 有没有一种简单的方法可以使用 XSLT 来检测和更正格式不正确的 XML?

javascript - 如何使用 XPath 选择具有特定 insideText 的直接同级元素?

sql - 在 Oracle SQL 中选择 xpath 值作为单独的行

selenium - Appium无法使用UIAutomator中的文本在移动应用程序中定位元素

jquery - Backbone 分页器 XML