2013-03-01 147 views
0

我有下面的XML: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"?> 
<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> 
      <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"?> 
<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> 

有沒有辦法做到這一點,任何人都可以幫助我嗎?

感謝

回答

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