我有一个大型XML文件,我正在尝试编写循环遍历整个节点集(PRESOL,COMBINE和FAIROPS)的XSLT,并删除名为DATE的所有子节点。然后,我想将父节点的DATE重新格式化为mm / dd / yyyy。这是我的代码,我使用的是1.0版:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="DATE">
<xsl:text>
</xsl:text>
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
<xsl:text disable-output-escaping="yes"></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DATE[preceding::DATE]"/>
我很难理解如何遍历整个节点集。我希望不要对父节点名称进行硬编码。目前,我的代码只处理第一个父节点。此外,如果父节点没有子节点,我的代码将删除其DATE节点,我不明白为什么。非常感谢任何帮助。
我的输入
<NOTICES>
<PRESOL>
<DATE>01242013</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>01242013</DATE>
<AGENCY><![CDATA[Department of the Interior]]></AGENCY>
<OFFICE><![CDATA[Fish and Wildlife Service]]></OFFICE>
<LOCATION><![CDATA[CGS-WO]]></LOCATION>
<ZIP>97232</ZIP>
</MOD>
</CHANGES>
</PRESOL>
<COMBINE>
<DATE>02102013</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>02092013</DATE>
<AGENCY><![CDATA[Department of the Navy]]></AGENCY>
<OFFICE><![CDATA[Military Sealift Command]]></OFFICE>
<LOCATION><![CDATA[MSC Norfolk]]></LOCATION>
</COMBINE>
<COMBINE>
<DATE>02092013</DATE>
<AGENCY><![CDATA[Department of Veterans Affairs]]></AGENCY>
<OFFICE><![CDATA[Grand Junction VAMC)]]></OFFICE>
<LOCATION><![CDATA[Veterans Affairs Medical Center]]></LOCATION>
</COMBINE>
<PRESOL>
<DATE>12272012</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>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<DATE>01112013</DATE>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<DATE>01142013</DATE>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<DATE>01142013</DATE>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
</CHANGES>
</PRESOL>
<FAIROPP>
<DATE>02082013</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>
<AGENCY><![CDATA[Department of the Navy]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
</CHANGES>
</FAIROPP>
</NOTICES>
期望的输出:
<NOTICES>
<PRESOL>
<DATE>01/24/2013</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>
<AGENCY><![CDATA[Department of the Interior]]></AGENCY>
<OFFICE><![CDATA[Fish and Wildlife Service]]></OFFICE>
<LOCATION><![CDATA[CGS-WO]]></LOCATION>
<ZIP>97232</ZIP>
</MOD>
</CHANGES>
</PRESOL>
<COMBINE>
<DATE>02/10/2013</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>02/09/2013</DATE>
<AGENCY><![CDATA[Department of the Navy]]></AGENCY>
<OFFICE><![CDATA[Military Sealift Command]]></OFFICE>
<LOCATION><![CDATA[MSC Norfolk]]></LOCATION>
</COMBINE>
<COMBINE>
<DATE>02/09/2013</DATE>
<AGENCY><![CDATA[Department of Veterans Affairs]]></AGENCY>
<OFFICE><![CDATA[Grand Junction VAMC)]]></OFFICE>
<LOCATION><![CDATA[Veterans Affairs Medical Center]]></LOCATION>
</COMBINE>
<PRESOL>
<DATE>12/27/2012</DATE>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
<CHANGES>
<MOD>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
<MOD>
<AGENCY><![CDATA[Department of the Air Force]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
</CHANGES>
</PRESOL>
<FAIROPP>
<DATE>02/08/2013</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>
<AGENCY><![CDATA[Department of the Navy]]></AGENCY>
<OFFICE><![CDATA[Pacific Air Forces]]></OFFICE>
<LOCATION><![CDATA[354 CONS - Eielson]]></LOCATION>
</MOD>
</CHANGES>
</FAIROPP>
</NOTICES>
答案 0 :(得分:0)
DATE[preceding::DATE]
不正确,因为preceding
轴包含不是当前节点的祖先的节点。因此,这将匹配整个文档中第一个以外的所有 DATE。你想说的是“当前DATE元素的任何父元素也有一个DATE元素”:
<xsl:template match="DATE[../ancestor::*/DATE]"/>
..
是为了确保我们与上下文节点(当前DATE
元素不匹配,如果它的任何祖先有一个DATE元素)。
<xsl:template match="DATE">
<xsl:element name="{name()}">
<xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
</xsl:element>
</xsl:template>
虽然在您的情况下甚至不需要 - 只需让模板直接匹配文本节点:
<xsl:template match="DATE/text()">
<xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
</xsl:template>
模板的最终版本:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DATE/text()">
<xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
</xsl:template>
<xsl:template match="DATE[../ancestor::*/DATE]"/>
</xsl:stylesheet>