0

我有一个很大的 XML 文件,我正在尝试编写循环遍历整个节点集(PRESOL、COMBINE 和 FAIROPS)并删除所有名为 DATE 的子节点的 XSLT。然后,我想将父节点的 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">&lt;</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
    <xsl:value-of select="concat(substring(., 1, 2), '/', substring(., 3, 2), '/', substring(., 5, 4))"/>
    <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
    <xsl:value-of select="name()"/>
    <xsl:text disable-output-escaping="yes">&gt;</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>
4

1 回答 1

0

DATE[preceding::DATE]不正确,因为preceding轴包含不是当前节点祖先的节点。因此,这将匹配整个文档中除第一个 DATE 之外的所有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>
于 2013-02-17T03:26:01.470 回答