Need xsl help

L

Larry Coon

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
 
L

Larry Coon

Larry Coon wrote:

Follow-up: I think I found my own answer. Rather than:

<xsl:element name = "@colname">

I needed:

<xsl:element name = "{@colname}">


Larry Coon
University of California
 
M

Magnus Henriksson

- - - - 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 - - - -

- - - - 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 - - - -

Using an identity transform:

--begin--

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="column">
<xsl:element name="{@colname}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@colname"/>

</xsl:transform>

--end--

// Magnus
 
L

Larry Coon

Magnus said:
Using an identity transform:

(rest snipped)

Thanks!

I'm now trying to figure out how to copy a comment node from the
source document to the target document.

I know I have to use <xsl:comment>, but I don't see how to select
the comments from the source document. Can you point me the right
direction?


Larry Coon
University of California
 
L

Larry Coon

Magnus said:
Using an identity transform:

One more question, if I may....what's the correct way to add newlines
to the result document? I'm getting output that looks like:

<result><row><column> . . .

and I want:

<result>
<row>
<column> . . .

or at least:

<result>
<row>
<column> . . .

I played with inserting:

<xsl:text>
</xsl:text>

which sort of did what I want, but not reliably. Is there
a better way to accomplish this? Thanks.


Larry Coon
University of California
 
P

Peter Flynn

Larry said:
(rest snipped)

Thanks!

I'm now trying to figure out how to copy a comment node from the
source document to the target document.

I know I have to use <xsl:comment>, but I don't see how to select
the comments from the source document. Can you point me the right
direction?

<xsl:template match="comment()">
<xsl:copy/>
</xsl:template>

///Peter
 
M

Magnus Henriksson

One more question, if I may....what's the correct way to add newlines
to the result document? I'm getting output that looks like:

<result><row><column> . . .

and I want:

<result>
<row>
<column> . . .

This can be controlled with <xsl:eek:utput indent="yes"/> (see
http://www.w3.org/TR/xslt#output).

The implementation of this instruction depends on the vendor, so using
different XSLT processors may result in different indentations (or none at
all). I would also caution against pretty printing the result tree, as it
may lead to unforeseen problems (after all, whitespace is significant in
XML).

If you want pretty printed results (for e.g. debugging purposes) you can do
something like this: In your production stylesheet, set <xsl:eek:utput
indent="no"/>. Then create a second (pretty printing) stylesheet that
imports your production stylesheet (<xsl:import href="production.xsl"/>. The
pretty printing stylesheet should then set <xsl:eek:utput indent="yes"/>.

Because of the import precedence rules, the <xsl:eek:utput/> element in the
imported stylesheet has lower precedence.

Something like this:

--production.xsl-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput indent="no"/>

...

</xsl:transform>


--pretty-production.xsl--

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:import href="production.xsl"/>

<xsl:eek:utput indent="yes"/>

</xsl:transform>


// Magnus
 
L

Larry Coon

Magnus Henriksson wrote:

(snipped)

Thanks Magnus & Johannes for your help. My output is
looking the way I want it to now.


Larry Coon
University of California
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top