如何使用 XSLT select 或删除节点 xml

How to select or delete node xml by using XSLT

如何使用 XSLT select 或删除节点 xml?我想通过比较 startDate 和 enddate

select category 节点

这是我的xml

<Book>
    <Book>
        <startDate>2005-02-14T00:00:00.000</startDate>
        <endDate>2015-01-31T00:00:00.000</endDate>
        <record>
            <location>XXX</location>
            <telephone>0891234</telephone>
            <category>
                <name>ABC</name>
                <startdate>2005-02-14</startdate>
                <endDate>2015-01-31</endDate>
            </category>
            <category>
                <name>XYZ</name>
                <startdate>2015-02-01</startdate>
                <endDate>9999-12-31</endDate>
            </category>
        </record>
        <Author>Manu</Author>
    </Book>
    <Book>
        <startDate>2005-02-01T00:00:00.000</startDate>
        <endDate>9999-12-31T00:00:00.000</endDate>
        <record>
            <location>XXX</location>
            <telephone>0891234</telephone>
            <category>
                <name>ABC</name>
                <startdate>2005-02-14</startdate>
                <endDate>2015-01-31</endDate>
            </category>
            <category>
                <name>XYZ</name>
                <startdate>2005-02-01</startdate>
                <endDate>9999-12-31</endDate>
            </category>
        </record>
        <Author>Liverpool</Author>
    </Book>
</Book>

我想 select category\category\startDate 等于 \book\startDate\category\endDate 等于 \book\endDate 的节点(忽略时间戳在 \book\startDate\book\endDate 仅公司年月日)

这是预期的结果

<Book>
    <Book>
        <startDate>2005-02-14T00:00:00.000</startDate>
        <endDate>2015-01-31T00:00:00.000</endDate>
        <record>
            <location>XXX</location>
            <telephone>0891234</telephone>
            <category>
                <name>ABC</name>
                <startDate>2005-02-14</startDate>
                <endDate>2015-01-31</endDate>
            </category>
        </record>
        <Author>Manu</Author>
    </Book>
    <Book>
        <startDate>2005-02-01T00:00:00.000</startDate>
        <endDate>9999-12-31T00:00:00.000</endDate>
        <record>
            <location>XXX</location>
            <telephone>0891234</telephone>
            <category>
                <name>XYZ</name>
                <startDate>2005-02-01</startDate>
                <endDate>9999-12-31</endDate>
            </category>
        </record>
        <Author>Liverpool</Author>
    </Book>
</Book>

我该怎么办?这是我的代码。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="category">
    <xsl:if test="//category/startDate = //Book/startDate and //category/endDate = //Book/endDate">
        <xsl:copy-of select="."/>
    </xsl:if>
    </xsl:template>
</xsl:stylesheet>

标识模板(代码中的第一个模板)加上一个空模板

<xsl:template match="category[not(startdate = substring(ancestor::Book/startDate, 1, 10) and endDate = substring(ancestor::Book/endDate, 1, 10))]"/>

不要复制要删除的 category 元素可能会有所帮助。

https://xsltfiddle.liberty-development.net/naZXVEJ