Flatten XML Document

F

FGB

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
 
J

Joris Gillis

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>

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,
 
N

Nobody

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>
 

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

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top