Problem with XSLT transfromation

B

Boris

Hi,

I have a question regarding XSLT . Is it possible to output elements
in an XML between two values.
Here is an XML example of my data :

<root>
<stations>
<station>
<name>A</name>
<dep>05:25</dep>
<pos>1</pos>
<id>56</id>
</station>
<station>
<name>B</name>
<dep>06:25</dep>
<pos>2</pos>
<id>35</id>
</station>
<station>
<name>c</name>
<dep>07:25</dep>
<pos>3</pos>
<id>78</id>
</station>
<station>
<name>D</name>
<dep>08:25</dep>
<pos>4</pos>
<id>134</id>
</station>
****
****

</stations>
<start_station>
<id>35</id>
</start_station>
<end_station>
<id>78</id>
</end_station>
</root>

My aim si to output all those <station> elements that are between
<start_station><id> and
<end_station><id> values. I tried with <xsl:variable> but it did not
work.
Any help will be very apriciated.

Thank you,

Boris
 
J

Joris Gillis

My aim si to output all those said:
<start_station><id> and
<end_station><id> values.
Hi,


A stylesheet like this will do the trick:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="stations">
<stations>
<xsl:apply-templates select="station[id &gt; //start_station/id and id &lt; //end_station/id]"/>
</stations>
</xsl:template>

<xsl:template match="station">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

regards,
 
B

Boris Zebalc

Hi Joris,

The problem is that id's are not sorted numbers and so start_id could be
175 and end_id = 17. The position of a station is defined with the <pos>
element and they are sorted by <pos>. I was trying to set a variable
with the <pos> value where start_station/id = stations/station/id but i
can't quit do that...is it posible to make such a variable in tghis way
and then use it?

<xsl:for-each select="stations/station">
<xsl:if test="id = ../../start_station_id">
<xsl:variable name="sid" select="id"/>
</xsl:if>
</xsl:for-each>
 
J

Joris Gillis

The problem is that id's are not sorted numbers and so start_id could be
175 and end_id = 17.
The problem is solved with this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:variable name="start_id">
<xsl:value-of select="//start_station[id &lt; //end_station/id]"/>
<xsl:value-of select="//end_station[id &lt; //start_station/id]"/>
</xsl:variable>

<xsl:variable name="end_id">
<xsl:value-of select="//start_station[id &gt; //end_station/id]"/>
<xsl:value-of select="//end_station[id &gt; //start_station/id]"/>
</xsl:variable>

<xsl:template match="stations">
<stations>
<xsl:apply-templates select="station[id &gt; $start_id and id &lt; $end_id]"/>
</stations>
</xsl:template>

<xsl:template match="station">
<xsl:copy-of select="."/>
</xsl:template>

The position of a station is defined with the <pos>
element and they are sorted by <pos>. I was trying to set a variable
with the <pos> value where start_station/id = stations/station/id but i
can't quit do that...is it posible to make such a variable in tghis way
and then use it?

I don't understand what could be the use, but the code would be:

<xsl:for-each select="stations/station">
<xsl:variable name="sid">
<xsl:if test="id = ../../start_station/id">
<xsl:value-of select="id"/>
</xsl:if>
</xsl:variable>
</xsl:for-each>

regards,
 
B

Boris Zebalc

Hi Joris,

Thank you for your extensive help, it helps me a lot, but it doesn't
solve my problem.
Maybe I wasn't very clear in my previous post.

An XML Example:
<train>
<stations>
<station>
<pos>0</pos>
<id>65</id>
</station>
<station>
<pos>1</pos>
<id>32</id>
</station>
<station>
<pos>2</pos>
<id>117</id>
</station>
<station>
<pos>3</pos>
<id>4</id>
</station>
<station>
<pos>4</pos>
<id>18</id>
</station>
<station>
<pos>5</pos>
<id>100</id>
</station>
</stations>
<start_station>
<id>117</id>
</start_station>
<end_station>
<id>18</id>
</end_station>
<traindata>...
<train>

In the output i need to get the following stations in the order that
they are in the XML file..

<station>
<pos>2</pos>
<id>117</id>
</station>
<station>
<pos>3</pos>
<id>4</id>
</station>
<station>
<pos>4</pos>
<id>18</id>
</station>


Maybe I'm not getting your solution right ?

With best regards ,

Boris
 
J

Joris Gillis

Thank you for your extensive help, it helps me a lot, but it doesn't
solve my problem.
Maybe I wasn't very clear in my previous post.
Ooh, I understand, interpreted your question totally wrong:)

You could probably use something like this:

<xsl:variable name="start_station_pos">
<xsl:value-of select="//station[id = //start_station/id]/pos"/>
</xsl:variable>

<xsl:variable name="end_station_pos">
<xsl:value-of select="//station[id = //end_station/id]/pos"/>
</xsl:variable>

<xsl:template match="stations">
<stations>
<xsl:apply-templates select="station[pos &gt;= $start_station_pos and pos &lt;= $end_station_pos]"/>
</stations>
</xsl:template>


I hope I finally got it right...

regards,
 
B

Boris Zebalc

Hi, Joris,

Thank you very much.
This works like a chadrm.

With best regards,
Boris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top