Flatten XML Document

Discussion in 'XML' started by FGB, Dec 18, 2004.

  1. FGB

    FGB Guest

    Due to the form my instance documents can (sometimes) take, I have a
    need to flatten my documents from:

    Document A
    ----------
    <rootElement>
    <element1 value="element1Value">
    <element2 value="element2Value">
    <element3>element3Text</element3>
    </element2>
    </element1>
    </rootElement>

    to:

    Document B
    ----------
    <rootElement>
    <element3 element1="element1Value"
    element2="element2Value">element3Text</element3>
    </rootElement>

    Bandwidth is one of my primary motivations for this approach, as I
    anticipate many messages of this type per second. Also all of the data
    items for Document A are not always available, so the transform from
    Document A to Document B would be applied in some cases but not in
    others.

    I have searched this group and seen example stylesheets for this type
    of transform (or similar).

    My question is about the the "style" of this approach. In general,
    does this seem like a reasonable approach to reducing the size of XML
    documents? By the way, I plan to compress the documents prior to
    transmission whether Doc. A or Doc. B.

    I welcome any comments.


    FGB
    FGB, Dec 18, 2004
    #1
    1. Advertising

  2. FGB

    Joris Gillis Guest

    > Due to the form my instance documents can (sometimes) take, I have a
    > need to flatten my documents


    Hi,

    The following stylesheet's output will be similar to your desired output:

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

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="*[text()]">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="parent::*" mode="reverse"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="*" mode="reverse">
    <xsl:attribute name="{local-name()}"><xsl:value-of select="@value"/></xsl:attribute>
    <xsl:apply-templates select="parent::*[not(self::rootElement)]" mode="reverse"/>
    </xsl:template>

    </xsl:stylesheet>

    > My question is about the the "style" of this approach. In general,
    > does this seem like a reasonable approach to reducing the size of XML
    > documents? By the way, I plan to compress the documents prior to
    > transmission whether Doc. A or Doc. B.


    I don't have experience with XML optimization. Binary-wise, Doc B is a little smaller (but that difference will vanish if you apply zip compression). DOM-wise , I think they absorb the same memory (I could be wrong).

    regards,

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Spread the wiki (http://en.wikipedia.org)
    Joris Gillis, Dec 18, 2004
    #2
    1. Advertising

  3. FGB

    Nobody Guest

    "FGB" <> wrote in news:1103385026.551380.45370
    @z14g2000cwz.googlegroups.com:

    > Due to the form my instance documents can (sometimes) take, I have a
    > need to flatten my documents from:
    >
    > Document A
    > ----------
    > <rootElement>
    > <element1 value="element1Value">
    > <element2 value="element2Value">
    > <element3>element3Text</element3>
    > </element2>
    > </element1>
    > </rootElement>
    >
    > to:
    >
    > Document B
    > ----------
    > <rootElement>
    > <element3 element1="element1Value"
    > element2="element2Value">element3Text</element3>
    > </rootElement>
    >
    > Bandwidth is one of my primary motivations for this approach, as I
    > anticipate many messages of this type per second. Also all of the data
    > items for Document A are not always available, so the transform from
    > Document A to Document B would be applied in some cases but not in
    > others.
    >
    > I have searched this group and seen example stylesheets for this type
    > of transform (or similar).
    >
    > My question is about the the "style" of this approach. In general,
    > does this seem like a reasonable approach to reducing the size of XML
    > documents? By the way, I plan to compress the documents prior to
    > transmission whether Doc. A or Doc. B.
    >
    > I welcome any comments.
    >
    >
    > FGB
    >
    >

    Is starting out with this an option?:
    <rootElement>
    <element1 value="element1Value"/>
    <element2 value="element2Value"/>
    <element3>element3Text</element3>
    </rootElement>
    Nobody, Dec 19, 2004
    #3
    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. Tony Prichard
    Replies:
    0
    Views:
    717
    Tony Prichard
    Dec 12, 2003
  2. Simon

    xsl to flatten xml nodes

    Simon, Apr 12, 2004, in forum: XML
    Replies:
    3
    Views:
    4,123
    Martin Honnen
    Apr 12, 2004
  3. delgados129
    Replies:
    2
    Views:
    550
    delgados129
    Apr 16, 2005
  4. Francis Avila
    Replies:
    0
    Views:
    458
    Francis Avila
    Nov 2, 2003
  5. Replies:
    2
    Views:
    381
    Steve Pope
    Sep 27, 2006
Loading...

Share This Page