使用XSLT进行XML转换 - 截断空节点

时间:2013-03-01 09:29:58

标签: xml xslt

我有以下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>

有没有办法做到这一点,任何人都可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是将样式表的结尾更改为:

<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(.))]"/>

这假设您要在文档中删除到处的空元素。如果您只想删除<EMP_ADDR>元素的空子项,则可以使用<xsl:template match="EMP_ADDR/*[not(normalize-space(.))]"/>