Link to home
Start Free TrialLog in
Avatar of JohnSaint
JohnSaint

asked on

Passing multiple XML documents to a single XSL Stylesheet

I have been searching for an approach to using XSLT and PHP. I want to extract my data from the database and convert it to XML (using XML_Query2XML). I have several elements that I want to create like this (e.g. navigation, table content..). Each of these elements are now returned as a seperate XML documents (not stored as  files but as objects/strings). I want to be able to pass these multiple elements to a single XSL stylesheet to transform them.

I have looked at combining all the XML docs into one big doc and passing the docs as parameter elements to the XSL but both of these seem more complex than it needs to be so I assume I am missing something. Any suggestions would be much apreciated.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

John: Please post an example of your code so we can see what you're doing now.  Thanks, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnSaint
JohnSaint

ASKER

Hi Ray,

I'll post what I've got but I am not sure what way to go hence the question and incomplete code snipets included below.

Firstly I have the php code. I have ommitted a couple of objects that generate elements such as navigation and the detail (in this case a table of players scores etc.) - these objects return 2 seperate XML results. I then want to transform the xml with a template - I have firstly created two xsl files (attached) at the moment instead of just the one (or a set of includes) which I am aiming for. I was assuming this was a pretty common thing to do but I havn't seen it described anywhere so I assume I am perhaps looking at the problem from the wrong angle.

Thanks for the XSLT reference, I dig throught that and see what I can learn.

John

<?php
 
  function DisplayBrowser ($xml,$xml2,$template) {
 
    // read XSL stylesheet data
    $xsl = new DOMDocument;
    $xsl->load($template);
 
    // initialize XSLT engine
    $xslp = new XSLTProcessor;
    
    // attach XSL stylesheet object
    $xslp->importStyleSheet($xsl); 
    
    // perform transformation
    header('Content-Type: text/html');
    $xslp->setParameter('', 'nav_xml', $xml1->saveXML());
    echo $xslp->transformToXML($xml2);
  }
 
  # Database connector.
  $mdb2 = MDB2::factory('mysql://root@localhost/xml');
 
  // Generate Navigation Element
  $nav = new Navigation($mdb2); # Assume this object returns a simple XML object
  $xml1 = $nav->GetXML();
 
  // Generate Players League
  $pl = new PlayersLeague($mdb2);
  $xml2 = $pl->GetXML(); # Assume this object returns a simple XML object
 
  // Display the results using the PlayersLeague template.
  DisplayBrowser($xml1, $xml2, 'PlayersLeague.xsl');
 
?>
 
------------------ PlayersLeague.xsl ------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="/">
  <html>
  <body>
    <h2>Players League</h2>
    <table border="1">
    <tr bgcolor="#9acd32">
      <th align="left">Posit</th>
      <th align="left">Player</th>
      <th align="left">Played</th>
      <th align="left">Won</th>
      <th align="left">Bust</th>
      <th align="left">Points</th>
    </tr>
    <xsl:for-each select="root/row">
    <tr>
      <td><xsl:value-of select="id"/></td>
      <td>
  <img src="image.php?id={id}" />
      </td>
      <td><xsl:value-of select="played"/></td>
      <td><xsl:value-of select="won"/></td>
      <td><xsl:value-of select="bust"/></td>
      <td><xsl:value-of select="points"/></td>
    </tr>
    </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
 
</xsl:stylesheet>
 
-------------------- Navigation.xsl ------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param nav_xml></xsl:param>
<?xml-stylesheet type="text/css" href="./fpt.css" ?>
 
<xsl:template match="/">
  <html>
  <head>
    <link rel="stylesheet" href="./fpt.css" type="text/css" />
  </head>
  <body>
    <div id="navigation">
    <table border="1">
    <xsl:for-each select="root/row">
    <tr>
      <td>
        <a>
          <xsl:apply-templates select="nav_xml" />
        </a>
      </td>
    </tr>
    </xsl:for-each>
    </table>
    </div>
<h1>The value of Name Parameter is "<xsl:value-of select="$nav_xml"/>"</h1>
 
  </body>
  </html>
</xsl:template>
 
<xsl:template match="name">
  <xsl:attribute name="href">
    <xsl:value-of select="../link"/>
  </xsl:attribute>
  <xsl:value-of select="."/>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

Hi,

As an update I've sorted my issue by combining all my xml documents into one single xml document then passing this on to XSLT for transformation.

John.
John: isn't that what I suggested when I said, "Concatenate the different XML docs, ..."

??

~Ray
I may be too late but this is what you should probably have done.



<?xml version="1.0"?>
<!DOCTYPE novel SYSTEM "/dtd/novel.dtd" [
<!ENTITY chap1 SYSTEM "mydocs/chapter1.xml">
<!ENTITY chap2 SYSTEM "mydocs/chapter2.xml">
<!ENTITY chap3 SYSTEM "mydocs/chapter3.xml">
<!ENTITY chap4 SYSTEM "mydocs/chapter4.xml">
<!ENTITY chap5 SYSTEM "mydocs/chapter5.xml">
]>
<novel>
  <header>
    ...blah blah...
  </header>
&chap1;
&chap2;
&chap3;
&chap4;
&chap5;
</novel>
        
Hi Ray,

My original question was what was the best approach to passing multiple screen elements into an XSL template so I was hoping for some guidance/discussion on possible solutions and which was the best approach to take (pros and cons if you like). It had occurred to me to bung everything into one large XML document but I thought there might be another simpler solution so I didn't really consider your replay as a complete answer.

Hi Bob,

does your solution require all the xml files to be written to disk before transformation? If so I want to avoid this step and pass the xml as  parameters.

John.
<<<If so I want to avoid this step and pass the xml as  parameters.>>>

If you have strings, your job is easy.  

xml1= "<a>Alpha</a>";
xml2= "<b>Beta</b>";
composite= "<Meta>"+xml1+xml2+"/Meta>";