0

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

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

谢谢

4

1 回答 1

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

于 2013-03-01T10:06:40.750 回答