0

日付の比較に基づいて新しい要素を追加しようとしています。親の DATE が過去 7 日以内であれば、新しい要素を追加したいと考えています。日付の比較を行うコードを書きましたが、どこに置くかがわかりません。現在、親の DATE を再フォーマットするのはテンプレートにありますが、これにより、日付要素内に新しい要素が作成されます。現在の要素の外に新しい要素を作成する方法はありますか? ありがとう。

私の入力

<?xml version="1.0" encoding="UTF-8"?>
<NOTICES>
    <PRESOL>
        <DATE>03012013</DATE>
        <AGENCY><![CDATA[Department of the Interior]]></AGENCY>
        <OFFICE><![CDATA[Fish and Wildlife Service]]></OFFICE>
        <LOCATION><![CDATA[CGS-WO]]></LOCATION>
        <ZIP>97232</ZIP>
        <CHANGES>
            <MOD>
                <DATE>01112013</DATE>
                <COUNTRY>US</COUNTRY>
            </MOD>
        </CHANGES>
     </PRESOL>
    <COMBINE>
        <DATE>03012013</DATE>
        <AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
        <OFFICE><![CDATA[Air Force Materiel Command]]></OFFICE>
        <LOCATION><![CDATA[Tinker OC-ALC - (Central Contracting)]]></LOCATION>
        <ZIP>73145-3015</ZIP>
    </COMBINE>
    <COMBINE>
        <DATE>03052013</DATE>
        <AGENCY><![CDATA[Department of the Navy]]></AGENCY>
        <OFFICE><![CDATA[Military Sealift Command]]></OFFICE>
        <LOCATION><![CDATA[MSC Norfolk]]></LOCATION>
    </COMBINE>
    <COMBINE>
         <DATE>03292013</DATE>
         <AGENCY><![CDATA[Department of Veterans Affairs]]></AGENCY>
         <OFFICE><![CDATA[Grand Junction VAMC)]]></OFFICE>
         <LOCATION><![CDATA[Veterans Affairs Medical Center]]></LOCATION>
    </COMBINE>
    <PRESOL>
        <DATE>03302013</DATE>
        <AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
        <OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
        <LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
        <CHANGES>
             <MOD>
                 <DATE>01112013</DATE>
                 <COUNTRY>US</COUNTRY>
             </MOD>
             <MOD>
                 <DATE>01112013</DATE>
                 <COUNTRY>UK</COUNTRY>
             </MOD>
             <MOD>
                 <DATE>01142013</DATE>
                 <COUNTRY>JAPAN</COUNTRY>
             </MOD>
         </CHANGES>
     </PRESOL>
     <FAIROPP>
         <DATE>04012013</DATE>
         <AGENCY><![CDATA[Department of the Navy]]></AGENCY>
         <OFFICE><![CDATA[Bureau of Medicine and Surgery]]></OFFICE>
         <LOCATION><![CDATA[NH Camp Pendleton]]></LOCATION>
         <ZIP>92055</ZIP>
         <CHANGES>
             <MOD>
                  <DATE>02122011</DATE>
                  <COUNTRY>JAPAN</COUNTRY>
             </MOD>
         </CHANGES>
    </FAIROPP>
</NOTICES>

望ましい出力:

<?xml version="1.0" encoding="UTF-8"?>
<NOTICES>
    <PRESOL>
        <DATE>03012013</DATE>
        <AGENCY><![CDATA[Department of the Interior]]></AGENCY>
        <OFFICE><![CDATA[Fish and Wildlife Service]]></OFFICE>
        <LOCATION><![CDATA[CGS-WO]]></LOCATION>
        <ZIP>97232</ZIP>
        <CHANGES>
            <MOD>
                <DATE>01112013</DATE>
                <COUNTRY>US</COUNTRY>
            </MOD>
        </CHANGES>
     </PRESOL>
    <COMBINE>
        <DATE>03012013</DATE>
        <AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
        <OFFICE><![CDATA[Air Force Materiel Command]]></OFFICE>
        <LOCATION><![CDATA[Tinker OC-ALC  -  (Central Contracting)]]></LOCATION>
        <ZIP>73145-3015</ZIP>
    </COMBINE>
    <COMBINE>
        <DATE>03052013</DATE>
        <AGENCY><![CDATA[Department of the Navy]]></AGENCY>
        <OFFICE><![CDATA[Military Sealift Command]]></OFFICE>
        <LOCATION><![CDATA[MSC Norfolk]]></LOCATION>
    </COMBINE>
    <COMBINE>
         <DATE>03292013</DATE>
         **<mostrecent>YES</mostrecent>**
         <AGENCY><![CDATA[Department of Veterans Affairs]]></AGENCY>
         <OFFICE><![CDATA[Grand Junction VAMC)]]></OFFICE>
         <LOCATION><![CDATA[Veterans Affairs Medical Center]]></LOCATION>
    </COMBINE>
    <PRESOL>
        <DATE>03302013</DATE>
        **<mostrecent>YES</mostrecent>**
        <AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
        <OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
        <LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
        <CHANGES>
             <MOD>
                 <DATE>01112013</DATE>
                 <COUNTRY>US</COUNTRY>
             </MOD>
             <MOD>
                 <DATE>01112013</DATE>
                 <COUNTRY>UK</COUNTRY>
             </MOD>
             <MOD>
                 <DATE>01142013</DATE>
                 <COUNTRY>JAPAN</COUNTRY>
             </MOD>
         </CHANGES>
     </PRESOL>
     <FAIROPP>
         <DATE>04012013</DATE>
         **<mostrecent>YES</mostrecent>**
         <AGENCY><![CDATA[Department of the Navy]]></AGENCY>
         <OFFICE><![CDATA[Bureau of Medicine and Surgery]]></OFFICE>
         <LOCATION><![CDATA[NH Camp Pendleton]]></LOCATION>
         <ZIP>92055</ZIP>
         <CHANGES>
             <MOD>
                  <DATE>02122011</DATE>
                  <COUNTRY>JAPAN</COUNTRY>
             </MOD>
         </CHANGES>
    </FAIROPP>
</NOTICES>

私のXSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs">
    <xsl:output omit-xml-declaration="yes" indent="yes" cdata-section-elements="AGENCY DESC CLASSCOD CONTACT DATE NAICS LINK OFFADD OFFICE SUBJECT ZIP AGENCY ZIP"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name="backdate1" select="current-date() -7*xs:dayTimeDuration('P1D')"/>
    <xsl:variable name="backdate" select="xs:date(substring($backdate1, 1, 10))"/>
    <!-- copy all nodes -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="DATE/text()">
<!-- format DATE mm/dd/yyyy -->
        <xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
    **<!-- add new node mostrecent if date is within the last 7 days -->
        <xsl:variable name="subtract_date" select="days-from-duration(xs:date(concat(substring(., 5, 4), '-', substring(., 1, 2), '-', substring(., 3, 2))) - xs:date(substring($backdate1, 1, 10)))"/>
        <xsl:if test="$subtract_date &gt;= 0">
        <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
        <xsl:text disable-output-escaping="yes">mostrecent</xsl:text>
        <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
        <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
        <xsl:text disable-output-escaping="yes">mostrecent</xsl:text>
        <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
        </xsl:if>**
    </xsl:template>

    <!-- keep only the parent date node and delete all children date nodes -->
    <xsl:template match="DATE[../ancestor::*/DATE]"/>

    <!-- add new node type to each child node -->
    <xsl:template match="NOTICES/child::node()">
    <xsl:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="name(.)"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    <xsl:apply-templates select="@*|node()"/>
        <type>
            <xsl:value-of select ="name(.)"/>
        </type>
    <xsl:text disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="name(.)"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    </xsl:template>
</xsl:stylesheet>
4

1 に答える 1

0

テキスト ノードではなく DATE 要素で一致する場合は、これを行うことができます。

    <xsl:template match="DATE">
        <xsl:variable name="dStr" select="string(.)" />
        <xsl:variable name="bdStr" select="string($backdate1)" />
        <!-- format DATE mm/dd/yyyy -->
        <xsl:copy>
          <xsl:value-of select="concat(substring($dStr, 1, 2), '/', 
                                       substring($dStr, 3, 2), '/', 
                                       substring($dStr, 5, 4))"/>
        </xsl:copy>
        <!-- add new node mostrecent if date is within the last 7 days -->
        <xsl:variable name="subtract_date" 
                      select="days-from-duration(xs:date(
                                          concat(substring($dStr, 5, 4), '-', 
                                                 substring($dStr, 1, 2), '-', 
                                                 substring($dStr, 3, 2))) - 
                              xs:date(substring($bdStr, 1, 10)))"/>
        <xsl:if test="$subtract_date &gt;= 0">
          <mostrecent>YES</mostrecent>
        </xsl:if>
    </xsl:template>
于 2013-04-02T20:59:34.500 に答える