简体   繁体   English

使用XSLT将标记值从一个XML复制到另一个XML

[英]Copying tag values from one XML to another using XSLT

I have two xml files, say file1.xml which is as follows: 我有两个xml文件,比如file1.xml ,如下所示:

<?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 file2.xml如下

<?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 : 我期望输出xml文件的形式:

output.xml 与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. 请帮我用xsl来获得所需的输出。 This is not a homework and I am trying to understand xslt. 这不是作业,我试图理解xslt。

If you don't know your XSLT processor version, then run this transform and report the results... 如果您不知道自己的XSLT处理器版本,请运行此转换并报告结果...

<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 XSLT 1.0解决方案

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): 当应用于此XML文档 (提供的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 : 并且提供的file2.xml位于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() . 使用标准的XSLT函数document()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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