我有以下 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() < 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>
有什么办法吗?有人可以帮我吗?
谢谢