【问题标题】:copying content of one xml file into another xml file using xslt使用 xslt 将一个 xml 文件的内容复制到另一个 xml 文件中
【发布时间】:2013-03-12 21:51:08
【问题描述】:

我有一个 xml 文件,其中标签的属性是其他 xml 文件的 src。

 <a>
    <b>
       <c src="other1.xml" name="other1"></c>
       <c src="other2.xml" name="other2"></c>
       <c src="other3.xml" name="other3"></c> 
   </b>
 </a>

我想将此 xml 文件的内容更改为以下格式

<a>
    <b>
       <other1> content of other1.xml </other1>
       <other2> content of other2.xml </other2>
       <other3> content of other3.xml </other3>
   </b>
</a>

我尝试使用 xsl:variable 并在其中存储 src 的值,但出现错误。

有人请提出解决方案....即使是提示也将不胜感激

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    应该这样做:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="c">
        <xsl:element name="{@name}">
          <xsl:apply-templates select="document(@src)" />
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    以下文件为other1.xml、other2.xml和other3.xml:

    <I xmlns="hello">
      <am some="" xml="" />
    </I>
    


    <I xmlns="hello">
      <amAlso xml="" />
    </I>
    


    <I>
      <am xml="as well" />
    </I>
    

    并使用您的示例 XML 作为输入运行,结果是:

    <a>
      <b>
        <other1>
          <I xmlns="hello">
            <am some="" xml="" />
          </I>
        </other1>
        <other2>
          <I xmlns="hello">
            <amAlso xml="" />
          </I>
        </other2>
        <other3>
          <I>
            <am xml="as well" />
          </I>
        </other3>
      </b>
    </a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多