2013-03-02 63 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>