1

我想使用 xslt 将两个 xml 文件合并为一个。

file1:

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

file1:

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

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

我想将参数传递给包含要合并的文件的 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>

我只得到一个空的“剪切”标签。怎么了?

4

3 回答 3

4

不使用 xsltproc 但xmllint

(编辑:xsltproc 也允许 xinclude)

--xinclude : 对文档输入进行 XInclude 处理

x1.xml

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

x2.xml

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

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>

跑:

$ xmllint -xinclude  x3.xml 
<?xml version="1.0"?>
<cut xmlns:xi="http://www.w3.org/2003/XInclude">
  <content1>content1</content1>
  <content2>content2</content2>
</cut>
于 2012-09-07T15:30:24.710 回答
1

无法重现问题

很可能document()代码中的两个函数都返回“nothing”——这意味着用作每个调用的第一个参数的 URI 不标识文件(无法找到/解析文件),或者文件不包含格式良好的 XML 文档。

于 2012-09-07T14:27:19.330 回答
0

这对我来说很好用 xalan 和 saxon 解析器在 document() 调用中具有硬编码路径。问题很可能是由于某种原因,您的 xsl 没有看到您的文档。

我怀疑源文档中的 xml 存在问题,因为这可能会引发错误。

于 2012-09-07T14:44:52.530 回答