XSLT Transform Attributes to Elements?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: XSLT Transform Attributes to Elements?

  1. #1
    Steven V. Gates Guest

    XSLT Transform Attributes to Elements?

    Is it possible to do the following?

    Given an ADODB.Recordset (ADO 2.5), transform the attribute based xml
    stream (where each column is an attribute of a z:row element) to an
    element based stream (where each column is it's own elemement node) ?

    For example, my query returns the following from SQL Server:
    <rs:data>
    <z:row ContactName="Rubble, Barney" StatusDate="2001-04-17" />
    </rs:data>

    I'd like to apply an XSLT stylesheet to transform it to this:

    <Data>
    <ContactName>Rubble, Barney</ContactName>
    <StatusDate>2001-04-17</StatusDate>
    </Data>

    The hard part that I've run into is that I'd like the stylesheet to be
    generic enough for any arbirtrary ADO sourced XML stream. I thought
    this would be easy since they all share the same structure... but I'm
    stuck on how to code a variable an <xsl:Element name="xxx"> in a way
    that inserts the current attribute name.

    Here's what I've come up with so far:

    <xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="*/rs:data" xml:space="default">
    <xsl:for-each select="z:row">
    <xsl:element name="Data">
    <xsl:for-each select="@*">
    <xsl:variable name="c" select="@name"/>
    <xsl:element name="$c">
    <xsl:value-of select="*"/>
    </xsl:element>
    </xsl:for-each>
    </xsl:element>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    However, when I run the transform I get an error that tells me that
    'this name cannot begin with the $ character'.

    Does anyone have a suggestion for me?
    TIA



  2. #2
    Steven V. Gates Guest

    Re: XSLT Transform Attributes to Elements?

    Here's a snippet authored by Chris Stafano that accomplishes the job:
    http://www.vbxml.com/snippetcentral/...20010206021310

    Thank you Chris!



  3. #3
    jimbo Guest

    Re: XSLT Transform Attributes to Elements?


    Bummer of a surname!!

  4. #4
    Lou Guest

    Re: XSLT Transform Attributes to Elements?


    Create a generic rsxform.xsl with this:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
    xmlns:rs="urn:schemas-microsoft-com:rowset"
    xmlns:z="#RowsetSchema">
    <xslutput indent="yes" method="xml"/>

    <xsl:template match="/">
    <RecordSetInfo>
    <xsl:apply-templates select="xml/rs:data/z:row"/>
    </RecordSetInfo>
    </xsl:template>

    <xsl:template match="xml/rs:data/z:row">
    <Row>
    <xsl:for-each select="@*">
    <xsl:element name="{name()}">
    <xsl:value-of select="."/>
    </xsl:element>
    </xsl:for-each>
    </Row>
    </xsl:template>

    </xsl:stylesheet>

    Then transform it using: outstr = xmldoc.transformnode(xsldoc)
    Your xmldoc and xsl docs should be created using:
    CreateObject("MSXML2.DOMDocument.3.0")


    sgates@bufkin.com (Steven V. Gates) wrote:
    >Is it possible to do the following?
    >
    >Given an ADODB.Recordset (ADO 2.5), transform the attribute based xml
    >stream (where each column is an attribute of a z:row element) to an
    >element based stream (where each column is it's own elemement node) ?
    >
    >For example, my query returns the following from SQL Server:
    ><rs:data>
    > <z:row ContactName="Rubble, Barney" StatusDate="2001-04-17" />
    ></rs:data>
    >
    >I'd like to apply an XSLT stylesheet to transform it to this:
    >
    ><Data>
    > <ContactName>Rubble, Barney</ContactName>
    > <StatusDate>2001-04-17</StatusDate>
    ></Data>
    >
    >The hard part that I've run into is that I'd like the stylesheet to be
    >generic enough for any arbirtrary ADO sourced XML stream. I thought
    >this would be easy since they all share the same structure... but I'm
    >stuck on how to code a variable an <xsl:Element name="xxx"> in a way
    >that inserts the current attribute name.
    >
    >Here's what I've come up with so far:
    >
    ><xslutput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    ><xsl:template match="*/rs:data" xml:space="default">
    > <xsl:for-each select="z:row">
    > <xsl:element name="Data">
    > <xsl:for-each select="@*">
    > <xsl:variable name="c" select="@name"/>
    > <xsl:element name="$c">
    > <xsl:value-of select="*"/>
    > </xsl:element>
    > </xsl:for-each>
    > </xsl:element>
    > </xsl:for-each>
    ></xsl:template>
    ></xsl:stylesheet>
    >
    >However, when I run the transform I get an error that tells me that
    >'this name cannot begin with the $ character'.
    >
    >Does anyone have a suggestion for me?
    >TIA
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center