簡體   English   中英

使用XSLT從XML到XML-轉換多個節點

[英]XML to XML using XSLT - transforming multiple nodes

我有以下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>

它給了我下面提到的輸出:

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

<?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的新手,將不勝感激

謝謝

這個簡短的轉換

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

這將達到目的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM