471,594 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to transform XML by using XSLT

Hi
I need to know how to transform a XML file by using a XSLT file. Consider the following XML file

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><LogFile><Dog TimeStamp="16.02.2004 10:04:48" ModifiedBy="Smith, Geoff"><NameOfDog>Fido</NameOfDog><Weight>10</Weight></Dog><Dog TimeStamp="16.02.2004 10:05:24" ModifiedBy="Scott, Al"><Color>Black</Color><Weight>12</Weight></Dog></LogFile

By the magic of some XSLT file I want the following output to be created.

Modified by: Smith, Geoff (16.02.2004 10:04:48
Name of the Dog: Fid
Weight: 1

Modified by: Scott, Al (16.02.2004 10:05:24
Color: Blac
Weight: 1

Does anybody know how this can be done? How can I output ALL elements of one node without having to name all children using <xsl:value-of select="NameOfElement" />, ..
Thank you a great lot!!

Daniel Walzenbach
Nov 12 '05 #1
2 1890
"Daniel Walzenbach" <da**********************@freudenberg.de> wrote in message
news:5C**********************************@microsof t.com...
I need to know how to transform a XML file by using a XSLT file. Consider the following XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><LogFile>
<Dog TimeStamp="16.02.2004 10:04:48" ModifiedBy="Smith,
Geoff"><NameOfDog>Fido</NameOfDog><Weight>10</Weight>
</Dog> : : Modified by: Smith, Geoff (16.02.2004 10:04:48)


Daniel,

You'll have to set the xsl:output element to set an output method of text,
and then it's simply a matter of placing the results of several XPath
expressions at the desired places in a document.

For starters,

- - - DogLog.xsl (excerpt)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:template match="LogFile/Dog">
<xsl:text>Modified by: </xsl:text>
<xsl:value-of select="@ModifiedBy" />
<xsl:text> (</xsl:text>
<xsl:value-of select="@TimeStamp" />
<xsl:text>)&#x0d;&#x0a;</xsl:text>
</xsl:template>

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

</xsl:stylesheet>
- - -

With text output, settings for whitespace preservation may conflict with
natural and clear indenting of XSL source, so rather than just use a basic
literal text document with preserved whitespace, I use xsl:text elements
above to precisely distinguish the literal text content I want emitted into
the target document from the other whitespace that is only used to
present a readable stylesheet.

In these text elements, you see the two character entities, #x0d; and #x0a;
which correspond to the carriage return and line feed (ie, the character
codes you may be more familiar with from C languages as "\r\n") that intro-
duce a newline into the text output.

There are two template rules in this stylesheet. The first matches the root
node of the document (which in XPath-thinking, is the node containing the
document element, LogFile) and applies the second template (iteratively)
over each record wrapped by the Dog element.

The xsl:value-of elements are how the result of XPath expressions against
the source document get injected into the result document. From this basic
skeleton, you should be able to extend it further to create text reports
formatted however you like.
Derek Harmon
Nov 12 '05 #2
Thank you Derek. I have to look for some kind of tutorial for this kind of stuff.

Best regard

Daniel
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Luther Miller | last post: by
1 post views Thread by Danny Lesnik | last post: by
4 posts views Thread by WStoreyII | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.