Solved

Passing multiple XML documents to a single XSL Stylesheet

Posted on 2008-06-19
11
1,511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
11 Comments
 
LVL 110

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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

724 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