简体   繁体   English

使用xslt合并两个xml文件

[英]merge two xml files using xslt

I would like to merge two xml files into one using xslt. 我想使用xslt将两个xml文件合并为一个。

file1:

<cut> <content1> .... </content1> </cut>

file1:

<cut> <content2> .... </content2> </cut>

merged:
<cut>
<content1> ... </content1>
<content2> ... </content2>
</cut>

I would like to pass parameters to the xslt containing the files to merge. 我想将参数传递给包含要合并文件的xslt。

xsltproc.exe" --stringparam file1 s:\\file1.xml --stringparam file2 s:\\file2.xml s:\\merge.xslt xsltproc.exe“ --stringparam file1 s:\\ file1.xml --stringparam file2 s:\\ file2.xml s:\\ merge.xslt

merge.xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    extension-element-prefixes="exsl">

  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="file1"/>
  <xsl:param name="file2"/>

  <xsl:variable name="big-doc-rtf">
    <xsl:copy-of select="document($file1)"/>
    <xsl:copy-of select="document($file2)"/>
  </xsl:variable>

  <xsl:variable name="big-doc" select="exsl:node-set($big-doc-rtf)"/>

  <xsl:template match="/">
    <cut>
      <xsl:apply-templates select="$big-doc/cut/*"/>
    </cut>
  </xsl:template>

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

  <xsl:template match="@*|text()|comment()|processing-instruction()">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

I only get an empty "cut" tag. 我只得到一个空的“ cut”标签。 What is wrong? 怎么了?

not using xsltproc but xmllint : 不使用xsltproc而是xmllint

(Edit: xsltproc also allows xinclude) (编辑:xsltproc也允许xinclude)

--xinclude : do XInclude processing on document input --xinclude:对文档输入进行XInclude处理

x1.xml x1.xml

<cut><content1>content1</content1></cut>

x2.xml x2.xml

<cut><content2>content2</content2></cut>

x3.xml x3.xml

<?xml version="1.0"?>
<cut xmlns:xi="http://www.w3.org/2003/XInclude">
  <xi:include href="x1.xml" parse="xml" xpointer="xpointer(/cut/content1)"/>
  <xi:include href="x2.xml" parse="xml" xpointer="xpointer(/cut/content2)"/>
</cut>

run: 跑:

$ xmllint -xinclude  x3.xml 
<?xml version="1.0"?>
<cut xmlns:xi="http://www.w3.org/2003/XInclude">
  <content1>content1</content1>
  <content2>content2</content2>
</cut>

Can't repro the problem . 无法重现问题

Most probably both document() functions in your code return "nothing" -- and this means that the URIs used as the 1st argument of each call don't identify a file (the file cannot be found/resolved), or the file doesn't contain a well-formed XML document. 您的代码中的两个document()函数很可能都返回“ nothing”(空)-这意味着用作每个调用的第一个参数的URI不能标识文件(无法找到/解析该文件),或者该文件不能包含格式正确的XML文档。

This worked fine for me with xalan and saxon parsers with hardcoded paths in the document() calls. 这对于xalan和saxon解析器在document()调用中具有硬编码路径的情况来说对我来说很好。 The problem is likely that, for some reason, your xsl is not seeing your documents. 问题可能是由于某种原因,您的xsl看不到您的文档。

I doubt that there is a problem with the xml in the source documents, since that would likely elicit an error. 我怀疑源文档中的xml是否存在问题,因为这很可能会引发错误。

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

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