Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XML to another XML format transformation using XSLT

5 views
Skip to first unread message

Binaryx

unread,
Apr 11, 2012, 5:31:57 AM4/11/12
to
I am new in xml, have read various documentations, but some practice
examples would be more helpful than theory. I need transform one XML
document to another XML document format(original standard) using XSLT
transformation. Can someone specify how to make this correctly? Also, is
there application that can do this?

Joe Kesselman

unread,
Apr 11, 2012, 8:42:59 AM4/11/12
to
There's a lot of good tutorial information on DeveloperWorks:
http://www.ibm.com/developerworks/xml/

Also, most XML parsers and XSLT processors come with some simple samples.

Read through those, and come back to us if you have specific questions.


--
Joe Kesselman,
http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."

Peter Flynn

unread,
Apr 14, 2012, 6:20:25 PM4/14/12
to
On 11/04/12 10:31, Binaryx wrote:
> I am new in xml, have read various documentations, but some practice
> examples would be more helpful than theory. I need transform one XML
> document to another XML document format (original standard)

What does "original standard" mean here?

> using XSLT
> transformation. Can someone specify how to make this correctly?

Not unless we know what the two XML formats are.

> Also, is there application that can do this?

Same answer.

XSLT is a language to let you write a transformation between formats. It
is not a ready-made transformer.

Here's an XML document from my notes app:

------------------------------ note.xml
<note date="2010-04-14">
<title>Shopping List</title>
<list>
<item>Sugar</item>
<item>Butter</item>
<item>Chocolate</item>
<item>Cream</item>
</list>
</note>

If I want this in XHTML, I would write something like:

-------------------------------------- note2html.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xhtml"/>

<xsl:template match="/">
<html>
<head>
<title>
<xsl:text>Note dated </xsl:text>
<xsl:value-of select="note/@date"/>
</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

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

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

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

</xsl:stylesheet>

Now I can run an XSLT2 processor like Saxon, eg

$ java -jar saxon9he.jar -o:note.html note.xml note2html.xsl

(or click something in an XML/XSLT IDE) and I get output like this:

-------------------------------------- note.html
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>Note dated 2010-04-14</title>
</head>
<body>
<h1>Shopping List</h1>
<ul>
<li>Sugar</li>
<li>Butter</li>
<li>Chocolate</li>
<li>Cream</li>
</ul>
</body>
</html>

The idea is that you specify (in an XSLT template) what to do whenever a
particular element type occurs (or a combination of elements types or
other nodes). The result that you construct (within each template) is a
combination of literal markup and XSLT features. An XSLT processor does
all the rest: reading and parsing the source file, identifying which
templates match which nodes, and building the result and putting it into
an output file.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
0 new messages