1
我有下面的XML:XML使用XML XSLT來 - 變換多個節點
<EMPLOYEE_LIST>
<EMPLOYEES>
<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>
</EMPLOYEE_LIST>
它給了我所提到的下輸出:
:<?xml version="1.0" encoding="UTF-8"?>
<employees>
<permanent>
<emp_id>650000</emp_id>
<f_name>KEITH</f_name>
<m_name>H</m_name>
<l_name>ROGERS</l_name>
</permanent>
<contractual>
<emp_id>650001</emp_id>
<f_name>DARRYL</f_name>
<m_name>Y</m_name>
<l_name>HANNAH</l_name>
</contractual>
</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="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>
</employee>
</employees>
我是新來的XSLT和任何幫助,將
感謝
感謝該做的伎倆,但只是一個簡單的問題 - 如果我有EMPLOYEE_LIST/EMPLOYEES_1(有自己的節點)的XML/EMPLOYEES_2(有自己的節點)/EMPLOYEES_3(帶有其自己的節點) - 這將是具有相似子節點的3個不同的父節點。我們如何將其轉化爲上述給定的輸出? – 2013-03-02 16:49:04
@ReggieMiller,我很高興「那樣做了」。請,因爲這解決了您的問題,請*,接受*這個答案 - 通過點擊答案旁邊的複選標記。另外,請問任何新的問題......好吧,新的SO問題。這有助於讀者和回答者,因爲評論格式不適合格式化代碼。 – 2013-03-02 16:52:12
按照指示完成。我現在將這個新場景作爲另一個問題發佈。謝謝 – 2013-03-02 17:00:48