Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Need xsl help

Thread view: 
Larry Coon - 13 Dec 2005 01:08 GMT
I'm trying to use xsl for the first time, and as with
any first-time attempt, I'm running into problems.

I'm trying to transform an XML document into another
XML document with a slightly different format.  Here's
an example of what the source XML looks like:

- - - - begin source.xml - - - -

<?xml version="1.0" encoding="UTF-8"?>

<result>
 <row>
   <column colname="device" colnum="1">syb0201</column>
   <column colname="physical" colnum="2">/dev/rlv0201</column>
   <column colname="dbname" colnum="3">curric_prod</column>
   <column colname="segname" colnum="4">index</column>
   <column colname="mbytes" colnum="5">500</column>
 </row>
 <row>
   <column colname="device" colnum="1">syb0201</column>
   <column colname="physical" colnum="2">/dev/rlv0201</column>
   <column colname="dbname" colnum="3">reports_prod</column>
   <column colname="segname" colnum="4">system</column>
   <column colname="mbytes" colnum="5">300</column>
 </row>
</result>

- - - - end of source.xml - - - -

(There are many more <row> elements, omitted for this example.

Here's what I want the target to look like:

- - - - target.xml - - - -

<?xml version="1.0" encoding="UTF-8"?>

<result>
 <row>
   <device colnum="1">syb0201</column>
   <physical colnum="2">/dev/rlv0201</column>
   <dbname colnum="3">curric_prod</column>
   <segname colnum="4">index</column>
   <mbytes colnum="5">500</column>
 </row>
 <row>
   <device colnum="1">syb0201</column>
   <physical colnum="2">/dev/rlv0201</column>
   <dbname colnum="3">reports_prod</column>
   <segname colnum="4">system</column>
   <mbytes colnum="5">300</column>
 </row>
</result>

- - - - end of target.xml - - - -

Here's the xsl I tried using:

- - - - begin transform.xsl - - - -

<?xml version="1.0"?>

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

 <xsl:template match = "/">
   <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match = "result">
   <result>
     <xsl:apply-templates/>
   </result>
 </xsl:template>

 <xsl:template match = "row">
   <row>
     <xsl:apply-templates/>
   </row>
 </xsl:template>

 <xsl:template match = "column">
   <xsl:element name = "@colname">
     <xsl:attribute name = "colnum">
       <xsl:value-of select = "@colnum"/>
     </xsl:attribute>

     <xsl:template match = "text()">
       <xsl:value-of select = "."/>
     </xsl:template>
   </xsl:element>
 </xsl:template>
</xsl:stylesheet>

- - - - end transform.xsl - - - -

When I apply the transform, it generates the <result> and <row>
elements just fine, but it doesn't generate the elements for the
columns.  I thought that this:

 <xsl:template match = "column">
   <xsl:element name = "@colname">
     <xsl:attribute name = "colnum">
       <xsl:value-of select = "colnum"/>
     </xsl:attribute>

would match a <column> element in the source, create a new
element in the target where the name of the new element is
the value of the colname attribute, and the colnum attribute
is copied verbatim.  But when I apply the transform, I get
stderr output:

Compiler warnings:
 file:///C:/java/transform.xsl: line 25: You
 cannot call an element '@colname'

And my output file looks like this:

- - - - begin out.xml - - - -

<?xml version="1.0" encoding="UTF-8"?><result>

<row>
syb0201
/dev/rlv0201
curric_prod
index
500
</row>
<row>
syb0201
/dev/rlv0201
reports_prod
system
300
</row>
</result>

- - - - end of out.xml - - - -

So it never creates the structure for the column element
(although it does copy the text inside the element correctly).
I don't understand the error "You cannot call an element
@colname," since as I understand it, this simply selects the
value of the colname attribute (right?).

If anybody more familiar with xsl than I can point me in the
right direction, I'd appreciate it.


Larry Coon
University of California
Rhino - 13 Dec 2005 04:49 GMT
> I'm trying to use xsl for the first time, and as with
> any first-time attempt, I'm running into problems.
>
> I'm trying to transform an XML document into another
> XML document with a slightly different format.  Here's
> an example of what the source XML looks like:

[snip]

I think you're on the wrong newsgroup. While there may be some people who
are fluent with XSL here, that is more by coincidence than anything else. I
seem to recall that there are some XML newsgroups, like comp.text.xml, that
would be more likely to get you the right answers.

Also, there are a number of free tutorials for XSL online; I remember
finding a few when I was looking several months back. Unfortunately, I've
forgotten most of the little I knew then so I can't help you myself but one
of the XML newsgroups or one of the XSL tutorials should help you get to the
bottom of your problem.

Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.