【问题标题】:Copy Xml node to another xml node using xsl使用 xsl 将 Xml 节点复制到另一个 xml 节点
【发布时间】:2017-10-02 21:03:17
【问题描述】:

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

&lt;title&gt; &lt;/title&gt; 移动到&lt;identAndStatusSection&gt; &lt;/identAndStatusSection&gt;

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

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

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

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

    修改&lt;identAndStatusSection&gt;模板并将其现有内容与&lt;title&gt;一起复制到&lt;commonInfo&gt;

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

    对于&lt;commonInfo&gt; 模板,复制除&lt;title&gt; 节点之外的所有节点。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多