xslt newbie: transform that involve relationship?

Discussion in 'XML' started by Zhang Weiwu, Oct 29, 2007.

  1. Zhang Weiwu

    Zhang Weiwu Guest

    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
     
    Zhang Weiwu, Oct 29, 2007
    #1
    1. Advertising

  2. Zhang Weiwu

    Pavel Lepin Guest

    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"
    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>
    </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
     
    Pavel Lepin, Oct 29, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isambella  via DotNetMonster.com

    Transform XML string using XSLT file

    Isambella via DotNetMonster.com, Aug 1, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    9,747
    Isambella via DotNetMonster.com
    Aug 2, 2005
  2. Igor
    Replies:
    1
    Views:
    4,976
    Dimitre Novatchev
    Jul 20, 2003
  3. David
    Replies:
    1
    Views:
    730
    Martin Honnen
    Apr 3, 2004
  4. Gary Huntress

    XSLT Transform Vector to Array

    Gary Huntress, Aug 13, 2004, in forum: XML
    Replies:
    6
    Views:
    1,115
    Robin Johnson
    Aug 17, 2004
  5. Ken Fine
    Replies:
    4
    Views:
    441
    Steven Cheng [MSFT]
    Aug 12, 2008
Loading...

Share This Page