简体   繁体   中英

Copying tag values from one XML to another using XSLT

I have two xml files, say file1.xml which is as follows:

<?xml version="1.0"?>
<root >
    <text id='a'>This is to be replaced</text>
    <note>This should not be touched</note>
    <text id='b'>This is intact</text>
</root>

file2.xml is as follows

<?xml version="1.0"?>
<root >
    <text id='a'>Replacement Text</text>
    <note>This is a personal note</note>
</root>

I expect an output xml file of the form :

output.xml

<?xml version="1.0"?>
<root>
    <text id='a'>Replacement Text</text>
    <note>This should not be touched</note>
    <text id='b'>This is intact</text>
</root>

Please help me with a xsl to get the desired output. This is not a homework and I am trying to understand xslt.

If you don't know your XSLT processor version, then run this transform and report the results...

<xsl:stylesheet version = "1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"> 
<xsl:output method = "text" /> 
<xsl:template match = "/" > 
 <xsl:text>Version :  </xsl:text><xsl:value-of select = "system-property('xsl:version')" /><xsl:text >&#x0A;</xsl:text> 
 <xsl:text>Vendor  :  </xsl:text><xsl:value-of select = "system-property('xsl:vendor')" /><xsl:text >&#x0A;</xsl:text> 
 <xsl:text>URL     :  </xsl:text><xsl:value-of select = "system-property('xsl:vendor-url')" /><xsl:text >&#x0A;</xsl:text> 
 <xsl:text>MS ver  :  </xsl:text><xsl:value-of select = "system-property('msxsl:version')" /><xsl:text >&#x0A;</xsl:text> 
 </xsl:template> 
</xsl:stylesheet>

XSLT 1.0 Solution

This is not tested, but it should work...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:param name="url-of-file2" />
<xsl:variable name="file2" select="document($url-of-file2)" />  

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

<xsl:template match="*[@id]">
  <xsl:variable name="ele" select="name()" />
  <xsl:variable name="id" select="@id" />
  <xsl:variable name="replacement-node" select="($file2//*[name()=$ele][@id=$id])[1]" />
  <xsl:choose>
    <xsl:when test="$replacement-node">
     <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:copy-of select="$replacement-node/text()"/>
      <xsl:apply-templates select="*|comment()|processing-instruction()"/>
     </xsl:copy>
    </xsl:when>
  <xsl:otherwise><xsl:call-template name="ident" /></xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

This transformation :

<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:variable name="vReps" select="document('file:///c:/temp/delete/file2.xml')/*/*[1]"/>

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

 <xsl:template match="text[@id='a']/text()">
  <xsl:value-of select="$vReps"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (the provided file1.xml):

<root>
    <text id='a'>This is to be replaced</text>
    <note>This should not be touched</note>
    <text id='b'>This is intact</text>
</root>

and having the provided file2.xml residing in c:\\temp\\delete\\file2.xml :

<root>
    <text id='a'>Replacement Text</text>
    <note>This is a personal note</note>
</root>

produces the wanted, correct result:

<root>
   <text id="a">Replacement Text</text>
   <note>This should not be touched</note>
   <text id="b">This is intact</text>
</root>

Explanation :

  1. Use and overriding of the identity rule .

  2. Use of the standard XSLT function document() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM