【问题标题】:XML transformation using XSLT - truncating null nodes使用 XSLT 进行 XML 转换 - 截断空节点
【发布时间】:2013-03-01 09:29:58
【问题描述】:

我有以下 XML:

<EMPLOYEE_LIST>
   <EMPLOYEES>
      <EMPLOYEE>
         <EMPID>650000</EMPID>
         <FIRST_NAME>KEITH</FIRST_NAME>
         <MIDDLE_NAME>HUTCHINSON</MIDDLE_NAME>
         <LAST_NAME>ROGERS</LAST_NAME>
         .
         .
         .
         .
         <EMP_ADDR>
            <STREET>A</STREET>
            <CITY>B</CITY>
            <STATE> </STATE>
            <ZIP>90210</ZIP>
            <COUNTRY>C</COUNTRY>
         </EMP_ADDR> 
         <EMP_ADDR>
            <STREET>G</STREET>
            <CITY>H</CITY>
            <STATE>I</STATE>
            <ZIP> </ZIP>
            <COUNTRY> </COUNTRY>
         </EMP_ADDR>
       </EMPLOYEE>
    </EMPLOYEES>
</EMPLOYEE_LIST>

它给了我下面提到的输出:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
      <employee>
         <emp_id>111345</emp_id>
         <f_name>KEITH</f_name>
         <m_name>HUTCHINSON</m_name>
         <l_name>ROGERS</l_name>
         .
         .
         .
         .
         <addresses>
            <addr>A</addr>
            <city>B</city>
            <province/>
            <postal>90210</postal>
            <country>C</country>
         </addresses>
         <addresses>
            <addr>G</addr>
            <city>H</city>
            <province>I</province>
            <postal/>
            <country/>
         </addresses>
      </employee>
</employees>

使用此 XSLT 进行转换时:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
        <xsl:template match="/EMPLOYEE_LIST">
            <employees>
                <xsl:apply-templates select="EMPLOYEES/node()"/>
            </employees>        
        </xsl:template>

        <xsl:template match="EMPLOYEE">
        <employee>
            <xsl:apply-templates select="*"/>
        </employee>
        </xsl:template>

        <xsl:template match="EMPLOYEE/EMPID">
        <emp_id>
            <xsl:value-of select="."/>
        </emp_id>
        </xsl:template>

        <xsl:template match="EMPLOYEE/FIRST_NAME">
        <f_name>
            <xsl:value-of select="."/>
        </f_name>
        </xsl:template>

        <xsl:template match="EMPLOYEE/MIDDLE_NAME">
            <m_name>
                <xsl:value-of select="."/>
            </m_name>
        </xsl:template>

        <xsl:template match="EMPLOYEE/LAST_NAME">
            <l_name>
                <xsl:value-of select="."/>
            </l_name>
        </xsl:template>
        .
        .
        .
        .
        .
       <xsl:template match="EMPLOYEE/EMP_ADDR[position() &lt; 5]">
        <addresses>
            <addr>
                <xsl:value-of select="normalize-space(STREET)"/>
            </addr>
            <city>
                <xsl:value-of select="normalize-space(CITY)"/>
            </city>
        <province>
                <xsl:value-of select="normalize-space(STATE)"/>
            </province>
            <postal>
                <xsl:value-of select="normalize-space(ZIP)"/>
            </postal>
            <country>
                <xsl:value-of select="normalize-space(COUNTRY)"/>
            </country>
        </addresses>
      </xsl:template>
</xsl:stylesheet>

这不是我想要实现的目标,因为我需要如下截断/删除空节点的输出:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
      <employee>
         <emp_id>111345</emp_id>
         <f_name>KEITH</f_name>
         <m_name>HUTCHINSON</m_name>
         <l_name>ROGERS</l_name>
         .
         .
         .
         .
         <addresses>
            <addr>A</addr>
            <city>B</city>
            <postal>90210</postal>
            <country>C</country>
         </addresses>
         <addresses>
            <addr>G</addr>
            <city>H</city>
            <province>I</province>
         </addresses>
      </employee>
</employees>

有什么办法吗?有人可以帮我吗?

谢谢

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    可能最简单的方法是将样式表的末尾更改为以下内容:

    <xsl:template match="EMPLOYEE/EMP_ADDR[position() &lt; 5]">
        <addresses>
            <xsl:apply-templates select="STREET"/>
            <xsl:apply-templates select="CITY"/>
            <xsl:apply-templates select="STATE"/>
            <xsl:apply-templates select="ZIP"/>
            <xsl:apply-templates select="COUNTRY"/>
        </addresses>
    </xsl:template>
    
    <xsl:template match="STREET">
        <addr>
            <xsl:value-of select="normalize-space(.)"/>
        </addr>
    </xsl:template>
    
    <xsl:template match="CITY">
        <city>
            <xsl:value-of select="normalize-space(.)"/>
        </city>
    </xsl:template>
    
    <xsl:template match="STATE">
        <province>
            <xsl:value-of select="normalize-space(.)"/>
        </province>
    </xsl:template>
    
    <xsl:template match="ZIP">
        <postal>
            <xsl:value-of select="normalize-space(.)"/>
        </postal>
    </xsl:template>
    
    <xsl:template match="COUNTRY">
        <country>
            <xsl:value-of select="normalize-space(.)"/>
        </country>
    </xsl:template>
    
    <!--
    Drop elements that don't have any text content after whitespace normalization
    -->
    <xsl:template match="*[not(normalize-space(.))]"/>
    

    这假设您想在文档中任何地方放置空元素。如果只想删除 &lt;EMP_ADDR&gt; 元素的空子元素,可以使用 &lt;xsl:template match="EMP_ADDR/*[not(normalize-space(.))]"/&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 2012-06-09
      • 2021-09-19
      相关资源
      最近更新 更多