XML to another XML format transformation using XSLT

B

Binaryx

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

Joe Kesselman

P

Peter Flynn

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:eek:utput 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
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top