?
Solved

Passing multiple XML documents to a single XSL Stylesheet

Posted on 2008-06-19
11
Medium Priority
?
1,521 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
8 Comments
 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 111

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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