xslt newbie: transform that involve relationship?

Z

Zhang Weiwu

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
 
P

Pavel Lepin

Zhang Weiwu said:
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"
xmlns:xsl="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>
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.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top