将xml作为参数传递给xslt并替换xml

San*_*jay 2 xslt

可能重复:
XML元素中的替换属性的XSLT是否存在于另一个XML中?

我有两个XML:

第一个XML:

<FirstXML>
  <catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
  </catalog>
</FirstXML>
Run Code Online (Sandbox Code Playgroud)

第二个XML:

<SecondXML>
  <catalog>
         <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
  </catalog>
</SecondXML>
Run Code Online (Sandbox Code Playgroud)

我想使用XSLT转换我的第一个XML.第一个xml的cd节点应该替换为第二个xml的cd节点.

从XSLT转换得到的XML:

<FirstXML>
  <catalog>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
  </catalog>
</FirstXML>
Run Code Online (Sandbox Code Playgroud)

请帮我解决XSLT问题.我想我们需要将Second XML作为参数传递给XSLT,我试图这样做.我是XSLT的新手,所以可能无法正确编码.关于我们如何做到这一点的任何意见都会有所帮助.提前致谢.

Dim*_*hev 6

我想我们需要将Second XML作为参数传递给XSLT.

这是可能的,但根本不是必需的.

更方便的方法是将文件路径传递给文档,并使用XSLT document()函数进行解析和访问:

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

 <xsl:param name="pDocLink" select=
  "'file:///c:/temp/delete/SecondXml.xml'"/>

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:copy-of select="document($pDocLink)/*/*[1]"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

在提供的"第一个"文档上应用此转换时:

<FirstXML>
    <catalog>
        <cd>
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>USA</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
        </cd>
    </catalog>
</FirstXML>
Run Code Online (Sandbox Code Playgroud)

而"第二"文件放在c:\temp\delete\SecondXml.xml:

<SecondXML>
    <catalog>
        <cd>
            <title>Hide your heart</title>
            <artist>Bonnie Tyler</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
        </cd>
    </catalog>
</SecondXML>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

<FirstXML>
   <catalog>
      <cd>
         <title>Hide your heart</title>
         <artist>Bonnie Tyler</artist>
         <country>UK</country>
         <company>CBS Records</company>
         <price>9.90</price>
         <year>1988</year>
      </cd>
   </catalog>
</FirstXML>
Run Code Online (Sandbox Code Playgroud)