我正在尝试根据日期比较添加新元素。如果父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 >= 0">
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:text disable-output-escaping="yes">mostrecent</xsl:text>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:text disable-output-escaping="yes"></</xsl:text>
<xsl:text disable-output-escaping="yes">mostrecent</xsl:text>
<xsl:text disable-output-escaping="yes">></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"><</xsl:text><xsl:value-of select="name(.)"/><xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:apply-templates select="@*|node()"/>
<type>
<xsl:value-of select ="name(.)"/>
</type>
<xsl:text disable-output-escaping="yes"></</xsl:text><xsl:value-of select="name(.)"/><xsl:text disable-output-escaping="yes">></xsl:text>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分: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 >= 0">
<mostrecent>YES</mostrecent>
</xsl:if>
</xsl:template>