Gabor Forgacs
Gabor Forgacs

Reputation: 505

merge two xml files using xslt

I would like to merge two xml files into one using xslt.

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.

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. What is wrong?

Upvotes: 1

Views: 3305

Answers (3)

Pierre
Pierre

Reputation: 35306

not using xsltproc but xmllint:

(Edit: xsltproc also allows xinclude)

--xinclude : do XInclude processing on document input

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>

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>

Upvotes: 4

Mike Girard
Mike Girard

Reputation: 464

This worked fine for me with xalan and saxon parsers with hardcoded paths in the document() calls. The problem is likely that, for some reason, your xsl is not seeing your documents.

I doubt that there is a problem with the xml in the source documents, since that would likely elicit an error.

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

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.

Upvotes: 1

Related Questions