簡體   English   中英

使用XSLT將標記值從一個XML復制到另一個XML

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

我有兩個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如下

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

我期望輸出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>

請幫我用xsl來獲得所需的輸出。 這不是作業,我試圖理解xslt。

如果您不知道自己的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解決方案

這沒有經過測試,但應該有效......

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

這種轉變

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

當應用於此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>

並且提供的file2.xml位於c:\\temp\\delete\\file2.xml

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

產生想要的,正確的結果:

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

說明

  1. 使用和覆蓋標識規則

  2. 使用標准的XSLT函數document()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM