p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Multiple XML files matched with common Number Id (http://p2p.wrox.com/showthread.php?t=14134)

gurbani June 12th, 2004 05:22 PM

Multiple XML files matched with common Number Id
 
Hi,


I'm new to XSLT and I'm strugling trying to get this done. I'd really appreciate any help.

I have multiple sources of XML. There is one file which contains the test definitions (test number, its name, and its unit). The other files contain experiments, etc. per each of these tests. I'm trying to develop an XSLT that transforms this into an HTML table (see below for a simplified example).


XML File 1: definitions.xml

<tests>
- <test number="1">
     <Name>Test 1</Name>
     <Unit>Celsius</Unit>
  </test>
- <test number="2">
  <Name>Test 2</Name>
  <Unit />
  </test>
- <test number="4">
  <Name>Test Blah</Name>
  <Unit>Km</Unit>
  </test>
...
<tests>

----------

XML File 2: experiments.xml
<experiment>
- <temperature value="25degC">
     <test number="1">
        <Sigma>0.23</Sigma>
        <Mean>10.44</Mean>
     </test>
     <test number="3">
        <Sigma>0.13</Sigma>
        <Mean>11.33</Mean>
     </test>
- <temperature value="50degC">
     <test number="1">
        <Sigma>1.22</Sigma>
        <Mean>13.13</Mean>
     </test>
     <test number="2">
        <Sigma>0.16</Sigma>
        <Mean>0.31</Mean>
     </test>
  ....


=======
OUTPUT should be something like this, where tests numbers will be mathched. If it is missing in any side, it'll still be output, but either with the test name/unit missing, or with the experiment data missing>

<table border=1 cellpadding=0>
<thead>
 <tr>
  <td rowspan=2>Test No</td>
  <td rowspan=2>Name</td>
  <td rowspan=2>Unit</td>
  <td colspan=2>25degC</td>
  <td colspan=2>50degC</td>
 </tr>
 <tr>
  <td>Mean</td> <td>Sigma</td> <td>Mean</td> <td>Sigma</td>
 </tr>
 <tr>
</thead>
<tbody>
 <tr>
    <td>1</td> <td>Test 1</td> <td>Celcius</td> <td>0.23</td> <td>10.44</td> <td>1.22</td> <td>13.13</td>
 </tr>
 <tr>
    <td>2</td> <td>Test 2</td> <td></td> <td></td> <td></td> <td>0.16</td> <td>0.31</td>
 </tr>
 <tr>
    <td>3</td> <td></td> <td></td> <td>0.13</td> <td>11.33</td> <td></td> <td></td>
 </tr>
 <tr>
    <td>4</td> <td>Test Blah</td> <td>Km</td> <td></td> <td></td> <td></td> <td></td>
 </tr>
</tbody>
</table




Thanks in advance for your help!

Kind regards,
  Prem

---
Prem Gurbani
Software Engineer,WW Mfg
Analog Devices Inc.

bmains June 12th, 2004 05:51 PM

Hello,

I think you may have a problem because you can't use one stylesheet for multiple XML files simultaneously. You can reuse stylesheets; however, it won't mix result sets as you have shown above. Can you combine the XML files?

Brian

gurbani June 12th, 2004 06:29 PM

Hi,

I couldn’t find anything about multiple XML sources either.

So I thought of developing a "higher-level" XML file (index.xml), which handles all different sources.. Then, one XSLT file accesses this “top level” xml which contains all the file names, etc. It seems to work, more or less, although I believe it’s too complex, and difficult to handle and maintain..

So I don’t know whether there is a cleaner and more elegant way of doing this… I’m with XSLT only a couple of weeks and I thought I may not be approaching it correctly.


The index.xml looks as follows:

<tables>
        <table id="Main">
                <head id="Test_No">
                        <source>definitions.xml</source>
                        <subhead>Test_No</subhead>
                </head>
                <head id="25degC">
                        <source>experiments.xml</source>
                        <subhead>Mean</subhead>
                        <subhead>Sigma</subhead>
                </head>
                <head id="50degC">
                        <source>experiments.xml</source>
                        <subhead>Mean</subhead>
                        <subhead>Sigma</subhead>
                </head>
....

(also, here I don't know how to make the head id dynamic, i.e. accept any temperature value, and either 1 or 4 different values)




Then, an XSLT. This is a simplified extract:

- <xsl:template name="showtable_values">
  <xsl:param name="tableid" />
- <xsl:for-each select="document('definitions.xml')/tests/test">
  <xsl:sort select="@number" order="ascending" />
  <xsl:variable name="mynumber" select="@number" />
- <xsl:variable name="myhref">
- <xsl:choose>
+ <xsl:when test="$tableid = 'Main'">
  <xsl:value-of select="concat('#Product_Anova', $mynumber)" />
  </xsl:when>
  </xsl:choose>
  </xsl:variable>
- <TR>
- <TD>
- <xsl:call-template name="showtable_number">
  <xsl:with-param name="href" select="$myhref" />
  <xsl:with-param name="text_link" select="$mynumber" />
  <xsl:with-param name="tableid" select="$tableid" />
  <xsl:with-param name="number" select="$mynumber" />
  </xsl:call-template>
  </TD>
- <TD>
  <xsl:value-of select="Name" />
  </TD>
- <TD>
  <xsl:value-of select="Unit" />
  </TD>
- <xsl:for-each select="document('../index.xml')/tables/table[@id = $tableid]/head">
  <xsl:variable name="myheadid" select="@id" />
  <xsl:variable name="mysource" select="source" />
- <xsl:if test="string($mysource) != 'definitions.xml'">
- <xsl:choose>
- <xsl:when test="string($mysource) = 'experiments.xml'">
  <xsl:variable name="mysourcenow" select="document(string($mysource))/*/test[@number = $mynumber]/*" />
- <xsl:for-each select="subhead">
  <xsl:variable name="mysubhead" select="text()" />
- <TD>
- <xsl:call-template name="showtable_matchvalue">
  <xsl:with-param name="matchsource" select="$mysourcenow" />
  <xsl:with-param name="matchsubhead" select="$mysubhead" />
  </xsl:call-template>
  </TD>
  </xsl:for-each>
  </xsl:when>
  </xsl:choose>
  </xsl:if>
  </xsl:for-each>
  </TR>
  </xsl:for-each>
  </xsl:template>



Sorry for the large amount of code.


Thanks again,
  Prem



---
Prem Gurbani
Software Engineer,WW Mfg
Analog Devices Inc.

Birger June 23rd, 2004 05:58 AM

I'm to have a look at the same issue. I found this link, maybe you too can get something from the examples:

http://www.topxml.com/xsltStylesheet...erversion=true

:-)


All times are GMT -4. The time now is 10:12 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.