![]() |
![]() | ![]() | ![]() | Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - triple xsl:for-each problem? Please any suggestion >Thread Next - Re: triple xsl:for-each problem? Please any suggestion Re: triple xsl:for-each problem? Please any suggestionTo: NULL Date: 7/8/2006 9:11:00 PM
"ina" <roberta.inalbon@g...> wrote in message
news:1152277641.303201.170200@m......
> Hello,
>
> I have a xml file with this structure
>
> <root>
> <rooms>
> <roomsA price="56"/>
> <date>26/02/2006</date>
> <roomsA>
> <roomA price="65"/>
> <date>26/03/2006</date>
> </roomsA>
>
> <roomB price="59">
> <date>26/02/2006</date>
> </roomB>
> <roomB price="75">
> <date>26/03/2006</date>
> </roomB>
> <roomC price="45">
> <date>26/02/2006</date>
> </roomC>
> <roomC price="75">
> <date>26/03/2006</date>
> </roomC>
> </rooms>
> </root>
>
>
> and I would like to obtain something like this
>
> <Room>
> <date>26/02/2006</date>
> <roomsAprice>56</roomsAprice>
> <roomsBprice>59</roomsBprice>
> <roomsCprice>45</roomsCprice>
> </Room>
> <Room>
> <roomsAprice>65</roomsAprice>
> <roomsBprice>75</roomsBprice>
> <roomsCprice>75</roomsCprice>
> <Date>26/03/2006</Date>
> </Room>
>
>
I'm afraid the XML you are starting off with is poor quality but your
proposed output is even worse.
The fact your initial example is inconsistent it's makes it difficult to
see what help you really need.
Might I suggest the clean output XML that is required is:-
<root>
<room date="26 Feb 2006">
<price type="A">56</price>
<price type="B">59</price>
<price type="C">45</price>
</room>
<room date="26 Mar 2006">
<price type="A">65</price>
<price type="B">75</price>
<price type="C">75</price>
</room>
</root>
It's not easy sorting out the date format choice that would be better
changed at source however the restructuring would look like this:-
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="dates" match="date" use="." />
<xsl:template match="/root/rooms">
<root>
<xsl:for-each select="*[count(key('dates',date)[1] | ./date) = 1]">
<room date="{date}">
<xsl:for-each select="key('dates', date)/parent::*">
<price band="{substring-after(local-name(),'room')}">
<xsl:value-of select="@price" />
</price>
</xsl:for-each>
</room>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
This assumes that your input really looks like this:-
<root>
<rooms>
<roomA price="56">
<date>26/02/2006</date>
</roomA>
<roomA price="65">
<date>26/03/2006</date>
</roomA>
<roomB price="59">
<date>26/02/2006</date>
</roomB>
<roomB price="75">
<date>26/03/2006</date>
</roomB>
<roomC price="45">
<date>26/02/2006</date>
</roomC>
<roomC price="75">
<date>26/03/2006</date>
</roomC>
</rooms>
</root>
Anthony.
> I tried to do xsl:each-for and doesn't work very well.
>
> <?xml version='1.0' ?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/">
> <xsl:for-each select="root/rooms">
> <Root>
> <xsl:for-each select="roomC">
> <xsl:for-each select="../roomB">
> <xsl:for-each select="../roomA">
> <rooms>
> <roomsAprice>
> <xsl:value-of select="."/>
> </roomsAprice>
> <roomsBprice>
> <xsl:value-of select="../roomB"/>
> </roomsBprice>
> <roomsCprice>
> <xsl:value-of select="../roomC"/>
> </roomsCprice>
> <Date>
> <xsl:value-of select="@date"/>
> </Date>
> </rooms>
> </xsl:for-each>
> </xsl:for-each>
> </xsl:for-each>
> </Root>
> </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
>
> Can someone help me on that?
>
> Ina
>
| ![]() | ![]() | ![]() |
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | |||||
|
