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
Solved

how to do a double loop with xsl:for-each??

Posted on 2003-12-11
7
1,177 Views
Last Modified: 2012-06-22
I got a xml output like below and wanna display something following the rules:

stooren/stoor@present = 'T' and stooren/stoor@id = flora/TXT

How to work with xsl file? Thanks in advance.

<flora><TXT>034</TXT><flora>
<flora><TXT>035</TXT><flora>
<flora><TXT>036</TXT><flora>

<stooren>
<stoor id="034" present="T"/>
<stoor id="035" present="F"/>
<stoor id="036" present="T"/>
</stooren>
0
Comment
Question by:starbuck
  • 3
  • 3
7 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 9922014
Not sure what you want to do, exactly, and your XML isn't well-formed (no root) but assuming your XML looks like:
<root>
<flora><TXT>034</TXT><flora>
<flora><TXT>035</TXT><flora>
<flora><TXT>036</TXT><flora>
<stooren>
<stoor id="034" present="T"/>
<stoor id="035" present="F"/>
<stoor id="036" present="T"/>
</stooren>
</root>

and your current context is the <root> element:

<xsl:for-each select="stooren/stoor[@present = 'T']">
    <xsl:for-each select="../../flora[TXT = current()/@id]">
        for each stoor, this selects the flora with the TXT element that's the same as the id attribute of the current stoor.
    </xsl:for-each>
</xsl:for-each>

If you can be a bit more specific about what your desired output is (perhaps with an example), I can help you better.  Also, I assume you're talking about a *single* XML document as in my example?

Regards,
Mike Sharp
0
 

Author Comment

by:starbuck
ID: 9922379
Hi Mike,

Thanks for your help. Based on above XML for the output I just display all elements value of flora child whoes TXT value is same with the id of stoor and 'T' value of present. In fact "T" means true and 'F' means false, TXT value is same with id value, and above xml results from 2 xml files. Now I just want to show the 'true' flora Do you understand it now? My email is enstbrest@yahoo.com that is also my MSN. Hope get help from you, XML expert.

regards,
Leon
0
 

Author Comment

by:starbuck
ID: 9922647
Hi Mike,

I just tried your way but doesnt work very well. Below is my detailed XML file:

<?xml version = '1.0'?>
<index>

<flora num="1"><ACA>Kromhals</ACA><TXT>0779</TXT><PAO>AC</PAO></flora>
<flora num="2"><ACA>Gewone ossentong</ACA><TXT>0054</TXT><PAO>CG</PAO></flora>
<flora num="3"><ACA>Bernagie</ACA><TXT>0147</TXT><PAO>AC</PAO></flora>
<flora num="4"><ACA>Akkervergeet-mij-nietje</ACA><TXT>0840</TXT><PAO>CD</PAO></flora>
<flora num="5"><ACA>Veldereprijs</ACA><TXT>1347</TXT><PAO>C</PAO></flora>
<flora num="6"><ACA>Grote ereprijs</ACA><TXT>1358</TXT><PAO>AC</PAO></flora>
<flora num="7"><ACA>Grasklokje</ACA><TXT>0198</TXT><PAO>C</PAO></flora>

<SOORTEN>
<SOORT id="779" present="F"/>
<SOORT id="54" present="F"/>
<SOORT id="147" present="F"/>
<SOORT id="840" present="T"/>
<SOORT id="1347" present="F"/>
<SOORT id="1358" present="T"/>
<SOORT id="198" present="F"/>
</SOORTEN>

</index>

Could you give me a detailed XSL codes with some explaination? Many thanks.

regards,
Leon
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 9924107
This XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-16" indent="yes"/>
    <xsl:template match="index">
        <table cellpadding="5px" cellspacing="0" border="1" style="border-collapse:collapse;">
            <tbody>
                <tr>
                    <th>ACA</th>
                    <th>PAO</th>
                </tr>
                <xsl:apply-templates select="SOORTEN/SOORT[@present='T']"/>
            </tbody>
        </table>
    </xsl:template>
    <xsl:template match="SOORT">
        <tr>
            <xsl:apply-templates select="/index/flora[number(TXT)= current()/@id]"/>
        </tr>
    </xsl:template>
    <xsl:template match="flora">
        <td>
            <xsl:value-of select="ACA"/>
        </td>
        <td>
            <xsl:value-of select="PAO"/>
        </td>
    </xsl:template>

</xsl:stylesheet>


Produces this output:

<table cellpadding="5px" cellspacing="0" border="1" style="border-collapse:collapse;">
    <tbody>
        <tr>
            <th>ACA</th>
            <th>PAO</th>
        </tr>
        <tr>
            <td>Akkervergeet-mij-nietje</td>
            <td>CD</td>
        </tr>
        <tr>
            <td>Grote ereprijs</td>
            <td>AC</td>
        </tr>
    </tbody>
</table>


Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9924132
By the way, I had to use the number() function because the @id and <TXT> don't exactly match because of the leading zero.

Regards,
Mike Sharp
0
 

Author Comment

by:starbuck
ID: 9924502
Hi, Mike,

Thanks very much. It does work but now I am thinking about another way. Because the above XML is combined dynamicly from 2 servers. Now I include one result to another one. How to do it directly without combination. It is to use document() to do it? Like below?

<xsl:apply-templates select="document(www.abc.com/flora.asp?soorten=$soorten)/SOORTEN/SOORT[@present='T']"/>

How to use it correctly?

regards,
Leon
0
 
LVL 10

Expert Comment

by:Yury_Delendik
ID: 10048980
<xsl:apply-templates select="document(concat('http://www.abc.com/flora.asp?soorten=', $soorten))/SOORTEN/SOORT[@present='T']"/>
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSL-FO - show results in 2 columns 5 32
Issue with XSLT mapping 10 41
PowerShell script to remove string in xml file 8 32
Powershell import xml to array (non-cli xml) 4 18
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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