Re: Editing XML
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Sun, 10 Jun 2007 22:10:13 -0500
Rob Kennedy wrote:
Hans-Peter Diettrich wrote:Rob Kennedy wrote:My first thought is to use XSLT to transform the original XML into a new XML document. Most of it would be a straight copy of the original nodes, but any nodes named <MSHelp:link> would be transformed into a corresponding <a> node instead.
This is what I'd like to try, but I'm bloody newbie with such scripts. A tutorial or other assistance would be nice...
OK. Um... heh. I was afraid you might ask about that. I'll get back to you later.
Here's the XSL document I used on the same test.xml file I talked about before. I named it transform.xsl.
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:style***
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:MSHelp="http://msdn.microsoft.com/mshelp"
>
<xsl:output
method="xml"
omit-xml-declaration="no"
version="1.0"
indent="yes"
/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="MSHelp:link">
<xsl:element name="a">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:style***>
The first xsl:template says to copy all nodes, as well as node attributes (the "@*" part). The second xsl:template provides a special case for MSHelp:link nodes. Instead of the default xsl:copy output, we get an xsl:element instead, and it includes a copy of all the MSHelp:link element's attributes and children.
To perform the transformation, I used Ant, the Java build tool, because that's the only way I know how to invoke XSL transformations. Here's my build.xml file:
<?xml version="1.0" encoding="US-ASCII"?>
<project name="Hans-Peter XML test" default="all" basedir=".">
<description>
Test renaming MSHelp:link nodes
</description>
<target name="all" description="Test output">
<echo message="Transforming XML..."/>
<xslt extension=".xml" in="test.xml" out="test-result.xml" style="transform.xsl"/>
</target>
</project>
I ran this on my school's Solaris machine because that's the only place I've ever done XSLT successfully, so I'm not sure what kind of Java environment you'd need. For me, it "just works." The result file seems to be the same as what I got from the Delphi program I showed before.
--
Rob
.
- Follow-Ups:
- Re: Editing XML
- From: Hans-Peter Diettrich
- Re: Editing XML
- References:
- Editing XML
- From: Hans-Peter Diettrich
- Re: Editing XML
- From: Rob Kennedy
- Re: Editing XML
- From: Hans-Peter Diettrich
- Re: Editing XML
- From: Rob Kennedy
- Editing XML
- Prev by Date: Re: Editing XML
- Next by Date: Re: Editing XML
- Previous by thread: Re: Editing XML
- Next by thread: Re: Editing XML
- Index(es):