Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Transform help required

From: "Joe Fawcett" <joefawcett@---------.------>
To: NULL
Date: 6/1/2008 9:55:00 AM



"chris f" <chrisfellows@n...> wrote in message 
news:#9WIDp7wIHA.1772@T......
> Can someone help me with the XSL to transform my XML document. The input 
> document's Record element can contain any number of paired Name[N] & 
> Value[N] elements where N starts at 1 and is incremented by 1 for each 
> subsequent pair of elements.
>
> Input:
> <Records>
>    <Record>
>        <Id>1</Id>
>        <Name1>SomeName10</Name1>
>        <Value1>SomeValue15</Value1>
>        <Name2>SomeName20</Name2>
>        <Value2>SomeValue25</Value2>
>    </Record>
>    <Record>
>        <Id>2</Id>
>             ...
>    </Record>
> </Records>
>
> Output:
> <MyRecords>
>    <MyRecord>
>        <Id>1</Id>
>        <Items>
>            <Item name="SomeName10" value="SomeValue15"/>
>            <Item name="SomeName20" value="SomeValue25"/>
>        </Items>
>    </MyRecord>
>    <MyRecord>
>        <Id>2</Id>
>        ...etc
>    </MyRecord>
> </MyRecords>
>
>
I think this stylesheet does the job:

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

  <xsl:template match="/">
    <MyRecords>
      <xsl:apply-templates select="Records/Record" />
    </MyRecords>
  </xsl:template>

  <xsl:template match="Record">
    <MyRecord>
      <xsl:copy-of select="Id"/>
      <Items>
        <xsl:apply-templates select="*[starts-with(local-name(), 'Name')]" 
/>
      </Items>
    </MyRecord>
  </xsl:template>

  <xsl:template match="*">
    <Item name="{.}" value="{following-sibling::*[1]}" />
  </xsl:template>
</xsl:stylesheet>

The original XML has a terrible structure, using a suffix on an element name 
always causes problems when processing, much better to have the same name 
with an attribute signifying the order:
<Name position="1">...</Name>


-- 

Joe Fawcett (MVP - XML)
http://joe.fawcett.name 



transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent