Solved

Passing multiple XML documents to a single XSL Stylesheet

Posted on 2008-06-19
11
1,504 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 108

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 108

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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now