Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT何时、复制和删除_Xml_Xslt - Fatal编程技术网

Xml XSLT何时、复制和删除

Xml XSLT何时、复制和删除,xml,xslt,Xml,Xslt,这是我的xml的一个简单版本: <?xml version="1.0" encoding="UTF-8"?> <level1> <d1> <date type="inclusive">1912-1987</date> <date type="bulk">1943-1987</date> </d1> <d1> <date type="inclusive"&g

这是我的xml的一个简单版本:

<?xml version="1.0" encoding="UTF-8"?>
<level1>

<d1>
    <date type="inclusive">1912-1987</date>
    <date type="bulk">1943-1987</date>
</d1>

<d1>
    <date type="inclusive">1962-1983</date>
    <date type="bulk">1962-1983</date>
</d1>

</level1>

1912-1987
1943-1987
1962-1983
1962-1983
我正在尝试编写一个脚本来比较这两个日期类型名称。如果bulk=inclusive,我希望它只保留inclusive并删除bulk。如果bulk和inclusive不同,我希望转换保持原样

这是我当前的转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" encoding="utf-8"/>
<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>
<xsl:variable name="inclusiveDate" select="//date[@type = 'inclusive']/text()"/>
<xsl:variable name="bulkDate" select="//date[@type = 'bulk']/text()"/>
<xsl:template match="//date[@type = 'bulk']">
    <xsl:choose>
        <xsl:when test="$bulkDate = $inclusiveDate">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:copy-of select="$inclusiveDate[not($bulkDate)]"/>
            </xsl:copy>
        </xsl:when>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

这就是它给我的:

<?xml version="1.0" encoding="utf-8"?>
<level1>
<d1>
    <date type="inclusive">1912-1987</date>
    <date type="bulk"/>
</d1>
<d1>
    <date type="inclusive">1962-1983</date>
    <date type="bulk"/>
</d1>
</level1>

1912-1987
1962-1983
我需要它来摆脱它

<date type="bulk"/>

如果与包含日期相同,则完全相同。如果批量日期不同,不要将其剔除,只留下如下内容:

<d1>
<date type="inclusive">1912-1987</date>
<date type="bulk">1943-1987</date>
</d1>

1912-1987
1943-1987

在这一点上,我尝试了很多不同的方法。

我想首先您应该匹配
d1
元素,这些元素具有匹配的
日期
元素:

d1[date[@type='bulk'] = date[@type='inclusive']]
然后匹配批量日期:

date[@type='bulk']
所有这些都可以组合成一个匹配项:

d1[date[@type='bulk'] = date[@type='inclusive']]/date[@type='bulk'] 
这,连同身份模板,应该给你想要的输出

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template 
    match="d1[date[@type='bulk'] = date[@type='inclusive']]/date[@type='bulk']"/>

</xsl:stylesheet>


工作小提琴:

谢谢,这很有效。我永远也想不到。