473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need xsl help

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
Dec 13 '05 #1
8 1365
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
Dec 13 '05 #2
"Larry Coon" <lc******@assist.org> wrote in message
news:43***********@assist.org...
- - - - 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
Dec 14 '05 #3
Magnus Henriksson wrote:
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
Dec 15 '05 #4
Magnus Henriksson wrote:
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
Dec 15 '05 #5
Larry Coon wrote:
I don't see how to select
the comments from the source document.


Have a look at the XPath specification
(<http://www.w3.org/TR/xpath#NT-NodeType>):

comment()
--
Johannes Koch
Spem in alium nunquam habui praeter in te, Deus Israel.
(Thomas Tallis, 40-part motet)
Dec 15 '05 #6
Larry Coon wrote:
Magnus Henriksson wrote:
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?


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

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Dec 16 '05 #7
"Larry Coon" <lc******@assist.org> wrote in message
news:43***********@assist.org...
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:output 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:output
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:output indent="yes"/>.

Because of the import precedence rules, the <xsl:output/> 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:output 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:output indent="yes"/>

</xsl:transform>
// Magnus
Dec 16 '05 #8
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
Dec 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
0
by: xunling | last post by:
i have a question about answering ..... this topic is "need help" what do i have to write at te topic line, !after i have klicked the "answer message" button ive tried many possibilities,...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.