Solved

Passing multiple XML documents to a single XSL Stylesheet

Posted on 2008-06-19
11
1,507 Views
Last Modified: 2013-11-18
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.
0
Comment
Question by:JohnSaint
  • 3
  • 3
  • 2
11 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 21822979
John: Please post an example of your code so we can see what you're doing now.  Thanks, ~Ray
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 21823105
One other thought (albeit untested but easy enough to test)... Concatenate the different XML docs, stripping off the extraneous <?xml version="1.0"?>

Some good reading here: http://en.wikipedia.org/wiki/XSLT

Standing by, ~Ray
0
 

Author Comment

by:JohnSaint
ID: 21823625
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

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:JohnSaint
ID: 21844915
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.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 21845354
John: isn't that what I suggested when I said, "Concatenate the different XML docs, ..."

??

~Ray
0
 
LVL 10

Expert Comment

by:BobSiemens
ID: 21848662
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>
        
0
 

Author Comment

by:JohnSaint
ID: 21863739
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.
0
 
LVL 10

Expert Comment

by:BobSiemens
ID: 21869000
<<<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>";
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question