Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2005, 10:09 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple XML Docs/One XSLT Stylesheet

I wanted to pull several XML docs into one XSLT stylesheet, that would then be transformed to XHTML using the transformNode method within an ASPX doc, and I came up with 2 great solutions after reading several articles:
Method #1
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<xsl:variable name="doc1" select="document('http://SERVER/DIRECTORY/docs/xml/testdoc1.xml')" />
<xsl:variable name="doc2" select="document('http://SERVER/DIRECTORY/docs/xml/testdoc2.xml')" />

    <xsl:template match="/">
    Method #1:<br />
        <xsl:for-each select="$doc1">
            <xsl:value-of select="home/header/seal_url"/>
         </xsl:for-each>
        <br /><br />

        <xsl:for-each select="$doc2">
            <xsl:value-of select="home/alert/alert_title"/>
         </xsl:for-each>
 
    </xsl:template>
</xsl:stylesheet>
Method #2
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    Method #2:<br />
        <xsl:for-each select="document('http://SERVER/DIRECTORY/docs/xml/testdoc1.xml')">
            <xsl:value-of select="home/header/seal_url" />
         </xsl:for-each>
        <br /><br />

        <xsl:for-each select="document('http://SERVER/DIRECTORY/docs/xml/testdoc2.xml')">
            <xsl:value-of select="home/alert/alert_title" />
         </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>
But both of these solutions require that the root elements' name be identical.

So now I'd like to know if it's possible to have 2 or more XML doc's with different root element names, like this:

xmldoc1.xml
<doc1>
 <something>
  <moreinfo>...</moreinfo>
 </something>
</doc1>

xmldoc2.xml
<doc2>
 <yadayada>
  <whatever>...</whatever>
 </yadayada>
</doc2>

...and then combine them into one, which would result in this output:
<combined>
 <doc1>
  <something>
   <moreinfo>...</moreinfo>
  </something>
 </doc1>

 <doc2>
  <yadayada>
   <whatever>...</whatever>
  </yadayada>
 </doc2>
</combined>

Thanks.

KWilliams
 
Old August 4th, 2005, 10:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Here's a complete stylesheet that will do what you want:

<combined xmlns:xsl="http://www.w3.org/1999/XSL/Transform xsl:version="1.0">
  <xsl:copy-of select="document('doc1.xml")"/>
  <xsl:copy-of select="document('doc2.xml")"/>
</combined>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 4th, 2005, 10:38 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great! Thanks Michael. I'll give it a try.

KWilliams
 
Old August 4th, 2005, 10:56 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, so I tried your suggestion, but I had a problem. I first copied your example into my stylesheet like this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet>
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
    <combined xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
        <xsl:copy-of select="document('testdoc1.xml')" />
        <xsl:copy-of select="document('testdoc2.xml')" />
    </combined>
</xsl:stylesheet>
...and then I also tried doing this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
    <combined>
        <xsl:copy-of select="document('testdoc1.xml')" />
        <xsl:copy-of select="document('testdoc2.xml')" />
    </combined>
</xsl:stylesheet>
...but both instances received this error message when parsed:
Keyword xsl:stylesheet may not contain combined. If you could let me know what I'm doing wrong, it would be very much appreciated. Thanks Michael.


KWilliams
 
Old August 4th, 2005, 11:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I gave you a complete stylesheet using the "simplified stylesheet" syntax. If you want to translate this to conventional stylesheet syntax, wrap it in an <xsl:stylesheet> element and then an <xsl:template match="/"> element, and remove the xsl:version attribute.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 4th, 2005, 11:54 AM
Banned
 
Join Date: Jul 2005
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. I didn't know that there was such a way of doing it. Every tutorial I've read so far just had the <xsl:stylesheet... method instead of a "simplified stylesheet" syntax. Thanks for letting me know, and thanks again for your help.

KWilliams





Similar Threads
Thread Thread Starter Forum Replies Last Post
apply-template from included xslt stylesheet chobo XSLT 9 April 16th, 2008 05:25 PM
Looking for developers for a XSLT stylesheet david_n XSLT 0 February 27th, 2007 10:03 AM
2 XML Docs > 1 XSLT Stylesheet kwilliams XSLT 3 September 12th, 2005 11:22 AM
Parsing an XSLT stylesheet failed .... :( Thebravehearth XML 0 October 12th, 2004 10:55 PM
Single HTTP POST - Multiple XML docs denniswong288 XML 1 August 26th, 2003 05:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.