Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 20th, 2005, 10: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

Reply With Quote
  #2 (permalink)  
Old June 20th, 2005, 12:13 PM
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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 03:58 AM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 09:50 AM
Help:Recursive Function - attributes into elements spencer.clark XSLT 1 June 10th, 2005 08:56 AM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 07:34 AM



All times are GMT -4. The time now is 03:22 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.