1
我有下面的XML:XML使用XML XSLT来 - 将类似的父节点
<EMPLOYEE_LIST>
<EMPLOYEES_01>
<PERMANENT>
<EMPID>650000</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>H</MIDDLE_NAME>
<LAST_NAME>ROGERS</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650001</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>Y</MIDDLE_NAME>
<LAST_NAME>HANNAH</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_01>
<EMPLOYEES_02>
<PERMANENT>
<EMPID>650002</EMPID>
<FIRST_NAME>KEITH</FIRST_NAME>
<MIDDLE_NAME>ROGERS</MIDDLE_NAME>
<LAST_NAME>H</LAST_NAME>
</PERMANENT>
<CONTRACTUAL>
<EMPID>650003</EMPID>
<FIRST_NAME>DARRYL</FIRST_NAME>
<MIDDLE_NAME>HANNAH</MIDDLE_NAME>
<LAST_NAME>Y</LAST_NAME>
</CONTRACTUAL>
</EMPLOYEES_02>
</EMPLOYEE_LIST>
,我使用下面的XML转换它:
<?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="PERMANENT">
<permanent>
<xsl:apply-templates select="*"/>
</permanent>
</xsl:template>
<xsl:template match="EMPID">
<emp_id>
<xsl:value-of select="."/>
</emp_id>
</xsl:template>
<xsl:template match="FIRST_NAME">
<f_name>
<xsl:value-of select="."/>
</f_name>
</xsl:template>
<xsl:template match="MIDDLE_NAME">
<m_name>
<xsl:value-of select="."/>
</m_name>
</xsl:template>
<xsl:template match="LAST_NAME">
<l_name>
<xsl:value-of select="."/>
</l_name>
</xsl:template>
<xsl:template match="CONTRACTUAL">
<permanent>
<xsl:apply-templates select="*"/>
</permanent>
</xsl:template>
<xsl:template match="EMPID">
<emp_id>
<xsl:value-of select="."/>
</emp_id>
</xsl:template>
<xsl:template match="FIRST_NAME">
<f_name>
<xsl:value-of select="."/>
</f_name>
</xsl:template>
<xsl:template match="MIDDLE_NAME">
<m_name>
<xsl:value-of select="."/>
</m_name>
</xsl:template>
<xsl:template match="LAST_NAME">
<l_name>
<xsl:value-of select="."/>
</l_name>
</xsl:template>
</xsl:stylesheet>
期待与转换后的XML以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
<type>permanent</type>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
<type>contractual</type>
<emp_id>650002</emp_id>
<f_name>KEITH</f_name>
<m_name>ROGERS</m_name>
<l_name>H</l_name>
<type>permanent</type>
<emp_id>650003</emp_id>
<f_name>DARRYL</f_name>
<m_name>HANNAH</m_name>
<l_name>Y</l_name>
<type>contractual</type>
</employee>
</employees>
到目前为止我还没有成功,因为我是新来的,任何帮助将是应用程序reciated
感谢
如果我将父节点名称从EMPLOYEES_02修改为ESTRANGED_02,您的建议是否可行? – 2013-03-02 19:53:45
@ReggieMiller,是的,只是试试:) – 2013-03-02 19:59:32
还没有尝试过整个XML,但你能告诉我,如果有一种方法可以在转换过程中截断/省去不需要的元素吗?假设我想在转换过程中截断l_name,以便生成的xml只有f_name/m_name?可以在此代码中完成吗? – 2013-03-02 20:48:56