Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2005, 09:32 AM
Authorized User
 
Join Date: Jan 2004
Location: Belfast, , United Kingdom.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default Elements and attributes

Hi Folks,

I am trying to convert an ADODB.RecordSet to a more readable form:

<rs:data>
    <z:row CarVidNo="33445566" CarRegNo="345rtgy" CarMake="Renault" CarModel="Clio" CarColour="Blue" CarEngine="1.2" CarPrice="5000"/>
    <z:row CarVidNo="55667788" CarRegNo="SXI 6002" CarMake="Vauxhall" CarModel="Corsa" CarColour="White" CarEngine="1.1" CarPrice="1000"/>
</rs:data>

Like So:

<z:row>
    <CarVidNo>33445566</CarVidNo>
    <CarRegNo>345rtgy</CarRegNo>
    <CarMake>Renault</CarMake>
    <CarModel>Clio</CarModel>
    <CarColour>Blue</CarColour>
    <CarEngine>1.2</CarEngine>
    <CarPrice>5000</CarPrice>
</z:row>

However, I want to the <CarPrice> node to remain as an attribute. I know that you can convert elements to attributes etc, but is there an easier way than converting all attributes to elements and then selecting the one element you want to turn back into in an attribute.

This is the style sheet I am using at the moment:

<xsl:import href="copy.xslt"/>

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />


      <xsl:template match="*[z:row]">


                <xsl:apply-templates/>

      </xsl:template>


        <xsl:template match="s:Schema">
                <xsl:for-each select="z:row">
                    <xsl:apply-templates />
                </xsl:for-each>
        </xsl:template>


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


        <xsl:template match="z:row">
          <xsl:element name="Cars">
                <xsl:apply-templates select="z:row"/>
          </xsl:element>
        </xsl:template>
</xsl:stylesheet>

And here is the copy stylesheet:

<xsl:template match="node( ) | @*">
  <xsl:copy>
    <xsl:apply-templates select="@* | node( )"/>
  </xsl:copy>
</xsl:template>


Sorta new to this XSLT stuff, so all help is appreciated.

Cheers,

Morris

 
Old June 20th, 2005, 11:13 AM
Authorized User
Points: 251, Level: 5
Points: 251, Level: 5 Points: 251, Level: 5 Points: 251, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Canada.
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rushman
Default

Hi!

I just dropped the namespaces from your source to make it simple (I didn't look for them ...)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="//row"></xsl:apply-templates>
        </root>
    </xsl:template>
    <xsl:template match="row">
        <xsl:copy>
            <xsl:attribute name="CarPrice">
                <xsl:value-of select="@CarPrice"/>
            </xsl:attribute>
            <xsl:for-each select="@*[name() != 'CarPrice']">
                <xsl:element name="{name()}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Dijkstra's law on Programming and Inertia:

If you don't know what your program is supposed to do, don't try to write it.




Similar Threads
Thread Thread Starter Forum Replies Last Post
remove empty elements but not ones with attributes dupdup XSLT 1 March 3rd, 2007 07:02 PM
how to control optional elements and attributes NEO1976 XSLT 5 September 4th, 2006 02:58 AM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 08:50 AM
Help:Recursive Function - attributes into elements spencer.clark XSLT 1 June 10th, 2005 07:56 AM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 06:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.