Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > xslt newbie: transform that involve relationship?

Reply
Thread Tools

xslt newbie: transform that involve relationship?

 
 
Zhang Weiwu
Guest
Posts: n/a
 
      10-29-2007
Dear all

As a newbie I don't know how to describe my issue clearly in right term.
I have read books about XSLT but I cannot find a way to do this kind of
transformation:

The system A produce XML output like this:


<branch name="shopA">
<yearly_revenue year="2006">
<categoryA>123000</category>
<categoryB>124000</category>
</yearly>
<yearly_revenue year="2007">
<categoryA>125000</category>
<categoryB>126000</category>
</yearly>
</branch>
<branch name="shopB">
<yearly_revenue year="2006">
<categoryA>123000</category>
<categoryB>124000</category>
</yearly>
<yearly_revenue year="2007">
<categoryA>125000</category>
<categoryB>126000</category>
</yearly>
</branch>

And system B take input XML like this:

<yearly_revenue year="2006">
<branch name="shopA">
<categoryA>123000</category>
<categoryB>124000</category>
</branch>
<branch name="shopB"
<categoryA>123000</category>
<categoryB>124000</category>
</branch>
</yearly_revenue>
<yearly_revenue year="2007">
<branch name="shopA">
<categoryA>125000</category>
<categoryB>126000</category>
</branch>
<branch name="shopB"
<categoryA>125000</category>
<categoryB>126000</category>
</branch>
</yearly_revenue>

Generally, the 'relationship' or how to categorize data, is changed.
The output XML file categorize by shops first, and by year second.
The second XML file cateogrize by year first, and by shop second.

I am not sure if XSLT can handle such transformation? Would be easy with
SQL with distinct selection, but probably is the weakness of XSLT
comparing to SQL? I could not find a 'select distinct' operation in XSLT
which would be necessary to do this kind of transformation.

Best regards and thanks in advance for hints!
Zhang Weiwu
 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      10-29-2007

Zhang Weiwu <> wrote in
<>:
> The system A produce XML output like this:


[snip "XML"]

No it doesn't. This is not well-formed. Are you sure you
want help from this group, or are you just aiming to annoy
people trying to help you?

> I am not sure if XSLT can handle such transformation?


Sure it can. It's one key and four simple templates:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="y" match="yearly" use="@year"/>
<xsl:template match="/">
<result>
<xsl:apply-templates
select=
"
data/branch/yearly[count(.|key('y',@year)[1])=1]
" mode="group"/>
</result>
</xsl:template>
<xsl:template match="yearly" mode="group">
<xsl:copy>
<xsl:apply-templates select="@year"/>
<xsl:apply-templates select="key('y',@year)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="yearly">
<branch>
<xsl:apply-templates select="../@name|*"/>
</branch>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

> Would be easy with SQL with distinct selection, but
> probably is the weakness of XSLT comparing to SQL?


XSLT is very different from SQL. XQuery is much closer to it
in terms of ideology and syntax but still doesn't bear
direct comparison. Hierarchical data and relational data
are simply too different for that.

--
It is rare to find learned men who are clean, do not stink,
and have a sense of humour. -- Liselotte in a letter to
Sophie, 30 Jul 1705
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
VS.NET 2008: controls that involve images render as red X in design time, how to fix? Ken Fine ASP .Net 4 08-12-2008 11:14 AM
Yet more exploitable IE flaws involve ActiveX, Codedreeamer Au79 Computer Support 0 04-28-2006 05:58 AM
Transform XML string using XSLT file Isambella via DotNetMonster.com ASP .Net 4 08-02-2005 07:19 AM
blocking I/O with javax.xml.parsers.DocumentBuilder.parse() and javax.xml.transform.Transformer.transform() jazzdman@gmail.com Java 1 03-27-2005 06:56 AM



Advertisments