[exslt] XML node replacement
Matthew Haynes
matt at matthaynes.net
Mon Aug 20 04:25:21 MDT 2007
Hi,
Sounds like you need an XSLT tutorial, there are some great ones out there
on the web:
http://www.google.co.uk/search?q=XSLT+tutorial
I could also recommend XSLT Programmers Reference by Michael Kay. This book
is pretty much the definitive guide to XSLT and can be picked up on Amazon
fairly cheaply.
As for your problem, below is how I would do it:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="
1.0">
<xsl:template match="/">
<test>
<!-- Apply templates to all <node1> nodes directly under /test/
-->
<xsl:apply-templates select="test/node1" />
<children>
<!-- Apply templates to all <node1> nodes directly under
/test/descendant -->
<xsl:apply-templates select="test/descendant::node()/node1"
/>
</children>
</test>
</xsl:template>
<!-- Match <node1> nodes -->
<xsl:template match="node1">
<!-- New element named after the value of doc_var_name -->
<xsl:element name="{./@doc_var_name}">
<!-- New element with value of doc_var_name -->
<xsl:attribute name="doc_var_name">
<xsl:value-of select="./@doc_var_name"/>
</xsl:attribute>
<!-- Copy of all child nodes-->
<xsl:copy-of select="./child::node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Cheers,
Matt
On 16/08/07, P Ram <emailram00 at gmail.com> wrote:
>
> Hi, I am a newbie to XSL and require help. I have two XMLs (see below) and
> must convert old to new .
>
> To summarize:
>
> 1. I have to replace all node1 with the attribute value for doc_var_name
> i.e. var1 (including those occuring within <test> and <child1><chil2> etc)
>
>
> 2. I have to remove all child1, child2 nodes and replace them with one
> instance of <children>.
>
> I am unable to figure this one out.
>
> Thanks!
> Terry
>
> -----------------------------------
> old format:
> ----------------
> <test>
> <node1 doc_var_name="var1>
> <a>1</a>
> </node1>
> <child1>
> <node1 doc_var_name="var2>
> <b>6</b>
> </node1>
> </child1>
> <child2>
> <node1 doc_var_name="var3>
> <c>3</c>
> </node1>
> </child2>
> </test>
>
>
> required new format:
> -----------------------------
> <test>
> <var1 doc_var_name="var1">
> <a>1</a>
> </var1>
> <children>
> <var2 doc_var_name="var2">
> <b>7</b>
> </var2>
> <var3 doc_var_name="var3">
> <c>3</c>
> </var3>
> </children>
> </test>
>
>
> _______________________________________________
> exslt mailing list
> list at exslt.org
> http://www.exslt.org/list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.fourthought.com/pipermail/exslt/attachments/20070820/f3a1884b/attachment.htm
More information about the exslt
mailing list