1

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?

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.