【问题标题】:XSLT 1.0 : Remove element based on attribute of the element matching an attribute of the parentXSLT 1.0:根据与父属性匹配的元素属性删除元素
【发布时间】:2017-02-01 12:06:51
【问题描述】:

我想在 XSLT 1.0 中根据必须匹配父属性的属性值将元素保留在 XML 中并删除其他元素

我只想保留属性 Journee 与父属性 Date 匹配的 DONNEES 元素。它可以是任何我不能放像 ='2015-09-17T06:00:00' 这样的日期。

这是 XML 示例

<?xml version="1.0"?>
<Root>
    <JOURNEE Date="2015-09-17T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
    <JOURNEE Date="2015-09-18T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
    <JOURNEE Date="2015-09-19T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
</Root>

这是我想要的输出

<Root>
<JOURNEE Date="2015-09-17T06:00:00">
<ID>
<DONNEES Journee="2015-09-17T06:00:00"/>
</ID>
</JOURNEE>
<JOURNEE Date="2015-09-18T06:00:00">
<ID>
<DONNEES Journee="2015-09-18T06:00:00"/>
</ID>
</JOURNEE>
<JOURNEE Date="2015-09-19T06:00:00">
<ID>
<DONNEES Journee="2015-09-19T06:00:00"/>
</ID>
</JOURNEE>
</Root>

这是我现在拥有的 XSLT,它不起作用,它会删除所有数据

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>  
</xsl:template>

<xsl:template match="/*/*/*DONNEES[(@Journee != /*/JOURNEE/@Date)]" />

我试过了,它可以工作,但我不能有这样的数据

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>  
</xsl:template>

<xsl:template match="/*/*/*DONNEES[(@Journee != '2015-09-17T06:00:00')]" />

谢谢你:)

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    您应该在表达式中使用相对路径来获取祖先日期

    试试这个 XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="xml" indent="yes" />
        <xsl:strip-space elements="*" />
    
        <xsl:template match="DONNEES[@Journee != ../../@Date]" />
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-22
      • 2016-03-02
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多