Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
XML
data.xmlanalysis
mappoint.webservice
msf
msxml-webrelease
netmyservices.sdk
passport.sdk
soap
soapsdk
uddi.general
uddi.programming
uddi.specification
xml
xmlsqlwebrelease
xsl
  
 
date: Sun, 1 Jun 2008 08:36:52 +0100,    group: microsoft.public.xsl        back       


Transform help required   
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>
date: Sun, 1 Jun 2008 08:36:52 +0100   author:   chris f

Re: Transform help required   
"chris f"  wrote in message 
news:#9WIDp7wIHA.1772@TK2MSFTNGP03.phx.gbl...
> 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
date: Sun, 1 Jun 2008 09:55:25 +0100   author:   Joe Fawcett am

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us