Groups > Design > Microsoft xsl > Re: Help with transform needed




Help with transform needed

Help with transform needed
Tue, 8 Apr 2008 19:06:17 +0100
I need some help to transform some input XML. In the input XML the FromDate 
and ToDate elements are identical within every Row element but, on the 
output XML, I want them to appear as attributes of the root node. Can 
someone help me out with the XSL?

Input XML:

<InputDataSet>
    <Row>
        <FromDate>21-03-2008 18:00</FromDate>
        <ToDate>22-03-2008 23:59</ToDate>
        <Data>12345</Data>
    </Row>
    <Row>
        <FromDate>21-03-2008 18:00</FromDate>
        <ToDate>22-03-2008 23:59</ToDate>
        <Data>6789</Data>
    </Row>
</InputDataSet>


Output XML:

<OutputDataSet FromDate="21-03-2008 18:00" ToDate="22-03-2008
23:59">
    <Row>
        <SomeData>12345</SomeData>
    </Row>
    <Row>
        <SomeData>6879</SomeData>
    </Row>
</OutputDataSet> 

Post Reply
Re: Help with transform needed
Wed, 9 Apr 2008 10:05:41 +0100
"chris fellows" <chrisfellows@nospam.co.uk> wrote in message 
news:e5lt%23MamIHA.4208@TK2MSFTNGP02.phx.gbl...
>I need some help to transform some input XML. In the input XML the FromDate

>and ToDate elements are identical within every Row element but, on the 
>output XML, I want them to appear as attributes of the root node. Can 
>someone help me out with the XSL?
>
> Input XML:
>
> <InputDataSet>
>    <Row>
>        <FromDate>21-03-2008 18:00</FromDate>
>        <ToDate>22-03-2008 23:59</ToDate>
>        <Data>12345</Data>
>    </Row>
>    <Row>
>        <FromDate>21-03-2008 18:00</FromDate>
>        <ToDate>22-03-2008 23:59</ToDate>
>        <Data>6789</Data>
>    </Row>
> </InputDataSet>
>
>
> Output XML:
>
> <OutputDataSet FromDate="21-03-2008 18:00"
ToDate="22-03-2008 23:59">
>    <Row>
>        <SomeData>12345</SomeData>
>    </Row>
>    <Row>
>        <SomeData>6879</SomeData>
>    </Row>
> </OutputDataSet>
>

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <OutputDataSet FromDate="{(*/Row/FromDate)[1]}" 
ToDate="{(*/Row/ToDate)[1]}">
      <xsl:apply-templates select="*/Row"/>
    </OutputDataSet>
  </xsl:template>

  <xsl:template match="Row">
    <Row><xsl:value-of select="Data"/></Row>
  </xsl:template>

</xsl:stylesheet>

-- 

Joe Fawcett (MVP - XML)

http://joe.fawcett.name

Post Reply
about | contact