2013-03-02 61 views
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和任何幫助,將

感謝

回答

1

這種短變換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="my:my" exclude-result-prefixes="my"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<my:Renames> 
    <n old="EMPLOYEES" new="employees"/> 
    <n old="EMPID" new="emp_id"/> 
    <n old="FIRST_NAME" new="f_name"/> 
    <n old="MIDDLE_NAME" new="m_name"/> 
    <n old="LAST_NAME" new="l_name"/> 
    <n old="PERMANENT" new="permanent"/> 
    <n old="CONTRACTUAL" new="contractual"/> 
</my:Renames> 

<xsl:variable name="vRenames" select="document('')/*/my:Renames/*"/> 

<xsl:template match="*/*" priority="-1"> 
    <xsl:element name="{$vRenames[@old = name(current())]/@new}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="PERMANENT|CONTRACTUAL"> 
    <xsl:apply-templates/> 
    <type><xsl:value-of select="$vRenames[@old = name(current())]/@new"/></type> 
</xsl:template> 
</xsl:stylesheet> 

當施加到所提供的XML文檔:

<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> 

產生想要的,正確的結果

<employees> 
    <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> 
</employees> 
+0

感謝該做的伎倆,但只是一個簡單的問題 - 如果我有EMPLOYEE_LIST/EMPLOYEES_1(有自己的節點)的XML/EMPLOYEES_2(有自己的節點)/EMPLOYEES_3(帶有其自己的節點) - 這將是具有相似子節點的3個不同的父節點。我們如何將其轉化爲上述給定的輸出? – 2013-03-02 16:49:04

+0

@ReggieMiller,我很高興「那樣做了」。請,因爲這解決了您的問題,請*,接受*這個答案 - 通過點擊答案旁邊的複選標記。另外,請問任何新的問題......好吧,新的SO問題。這有助於讀者和回答者,因爲評論格式不適合格式化代碼。 – 2013-03-02 16:52:12

+0

按照指示完成。我現在將這個新場景作爲另一個問題發佈。謝謝 – 2013-03-02 17:00:48

0

這將這樣的伎倆讚賞:

<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"> 
    <xsl:apply-templates select="*"/> 
    <type>permanent</type> 
    </xsl:template> 

    <xsl:template match="CONTRACTUAL"> 
    <xsl:apply-templates select="*" /> 
    <type>contractual</type> 
    </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> 

請注意,您只需要定義每個模板匹配EMPID,FIRST_NAME等。

當您的樣品輸入運行,這產生:

<employees> 
    <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> 
</employees>