使用xsl将Xml节点复制到另一个xml节点

时间:2017-10-02 21:03:17

标签: html xml xslt

我的XML文件格式如下。它需要在转换为HTML之前进行更改,

XML文件包含。

<dmodule>
      <identAndStatusSection>
      <dmAddress>            
          <dmIdent>

          </dmIdent>
          <dmAddressItems>
            <issueDate day="01" month="11" year="2013"/>
            <dmTitle>
              <techName>LP Compressor (LPC)  Fan Module</techName>
              <infoName>Disassemble Procedure</infoName>
            </dmTitle>
          </dmAddressItems>                     
        </dmAddress>
       </identAndStatusSection>
      <content>
    <procedure>
    <commonInfo>
    <title>           
    <inlineSignificantData significantParaDataType="psd51">TASK 72-31-00-030-001</inlineSignificantData> LP Compressor (LPC) Fan Module - Disassemble
    </title>
    <commonInfoDescrPara>
    <title>General</title>
    <para>This TASK gives the procedure to disassemble the LP Compressor (Fan) Module.</para>

    </commonInfoDescrPara>
    </commonInfo>
    </procedure>
    </content>
    </dmodule>

我想格式化XML,并将标题节点复制到identAndStatusSection标记

     <dmodule>
      <identAndStatusSection>
      <dmAddress>            
          <dmIdent>

          </dmIdent>
          <dmAddressItems>
            <issueDate day="01" month="11" year="2013"/>
            <dmTitle>
              <techName>LP Compressor (LPC)  Fan Module</techName>
              <infoName>Disassemble Procedure</infoName>
            </dmTitle>
          </dmAddressItems>                     
        </dmAddress>
        <title>           <inlineSignificantData significantParaDataType="psd51">TASK 72-31-00-030-001</inlineSignificantData> LP Compressor (LPC) Fan Module - Disassemble
</title>
       </identAndStatusSection>
      <content>
    <procedure>
    <commonInfo>

    <commonInfoDescrPara>
    <title>General</title>
    <para>This TASK gives the procedure to disassemble the LP Compressor (Fan) Module.</para>

    </commonInfoDescrPara>
    </commonInfo>
    </procedure>
    </content>
    </dmodule>

<title> </title>移至<identAndStatusSection> </identAndStatusSection>

我如何使用XSL表单执行此操作?

1 个答案:

答案 0 :(得分:0)

您可以从identity transform开始,将原始输入XML内容复制到输出中。

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

修改<identAndStatusSection>模板并将其现有内容与<title>

下的<commonInfo>一起复制
<xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    <xsl:apply-templates select="//commonInfo/title" />
</xsl:copy>

对于<commonInfo>模板,复制除<title>节点以外的所有节点。

<xsl:copy>
    <xsl:apply-templates select="*[not(self::title)]" />
</xsl:copy>

完整的XSLT和输出如下。

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

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="identAndStatusSection">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <xsl:apply-templates select="//commonInfo/title" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="commonInfo">
        <xsl:copy>
            <xsl:apply-templates select="*[not(self::title)]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

输出

<dmodule>
    <identAndStatusSection>
        <dmAddress>
            <dmIdent />
            <dmAddressItems>
                <issueDate day="01" month="11" year="2013" />
                <dmTitle>
                    <techName>LP Compressor (LPC) Fan Module</techName>
                    <infoName>Disassemble Procedure</infoName>
                </dmTitle>
            </dmAddressItems>
        </dmAddress>
        <title>
            <inlineSignificantData significantParaDataType="psd51">
                TASK 72-31-00-030-001
            </inlineSignificantData>
            LP Compressor (LPC) Fan Module - Disassemble
        </title>
    </identAndStatusSection>
    <content>
        <procedure>
            <commonInfo>
                <commonInfoDescrPara>
                    <title>General</title>
                    <para>This TASK gives the procedure to disassemble the LP Compressor (Fan) Module.</para>
                </commonInfoDescrPara>
            </commonInfo>
        </procedure>
    </content>
</dmodule>