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>

   
koleto115Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
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
Gertone (Geert Bormans)Information ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.