Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

How to organize correctlly this example XSL transformation?

I have xml input file:

<Event>
   <Result id='1'></Result>
   <Result id='2'></Result>
   <Result id='3'></Result>
   <Result id='4'></Result>
   <EventData>
      <Data id='1'></Data>
      <Data id='2'></Data>
      <Data id='3'></Data>
      <Data id='4'></Data>
   </EventData>
</Event>

The transformed HTML file should shows:

<h2>Even:</h2>
Bla bla
<h2> EventData:</h2>
 <ul>
   <li>Data (1) : Result(1) </li>                
    <li>Data (2) : Result(2) </li>
...
<ul>

   
0
koleto115
Asked:
koleto115
  • 2
2 Solutions
 
Geert BormansCommented:
This could work
If the file gets big, I recommend indexing, using a key
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="Event">
    <html>
        <body>
            <h2>Event:</h2>
            <p>blabla</p>
            <h2>EventData:</h2>
            <ul>
                <xsl:apply-templates select="EventData/Data"/>
            </ul>
        </body>
    </html>
</xsl:template>
    <xsl:template match="Data">
        <li>
            <xsl:value-of select="."/>
        <xsl:text> : </xsl:text>
        <xsl:value-of select="ancestor::Event/Result[@id = current()/@id]"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
koleto115Author Commented:
Thanks.

There should by not more then 200 result nodes. Do I still have to use key?
If so where can I find an example?
0
 
Geert BormansCommented:
Well, it would provide you with nicer code anyway
(200 results should not be a problem)
Here is the example, the previous is easier to understand,
the new one is what I would use in a production environment
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="res" match="Event/Result" use="@id"/>
    <xsl:template match="Event">
        <html>
            <body>
                <h2>Event:</h2>
                <p>blabla</p>
                <h2>EventData:</h2>
                <ul>
                    <xsl:apply-templates select="EventData/Data"/>
                </ul>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Data">
        <li>
            <xsl:value-of select="."/>
            <xsl:text> : </xsl:text>
            <xsl:value-of select="key('res', @id)"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now