wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Adding a new element to xml using xslt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Adding a new element to xml using xslt" Watch "Adding a new element to xml using xslt" New topic
Author

Adding a new element to xml using xslt

N Bhonsle
Greenhorn

Joined: Jan 12, 2005
Posts: 13
Hi:
I have the following xml snippet --
<abc:configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://oracle.com/xmlns/abcst/configuration configuration.xsd" locale="en" webcache="false" rmi="true">
<started status="true"></started>
<server></server>
..
....
</abc:configuration>

I need to convert this to the following xml:
<abc:configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://oracle.com/xmlns/abcst/configuration configuration.xsd" webcache="false" useMgmt="true" metadata="true">

<drillOutDir></drillOutDir>
<started status="true"></started>
<server></server>
.....
</abc:configuration>

I have the following xsl, that converts the initial snippet to the resultant but i observed that the <started> element occurs before the <drillOutDir> element. How can I change my xsl to achieve the above xml in the exact order?

<xsl:template match="abc:configuration">
<xsl:copy>
<xsl:copy-of select="@*[local-name() != 'rmi' and local-name() != 'locale']"/>
<xsl:attribute name="useMgmt">
<xsl:text>true</xsl:text></xsl:attribute>
<xsl:attribute name="metadata"><xsl:text>true</xsl:text></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
<xsl:text></xsl:text>
</xsl:template>

<xsl:template match="server">
<xsl:element name="drillOutDir">
</xsl:element>
</xsl:template>

Please help.
Thanks.
N Bhonsle
Greenhorn

Joined: Jan 12, 2005
Posts: 13
I have come to a point using the xslt in which I can get the "order" of the nodes preserved ie the "started" element occurs before the "drillOutDir" element but the attributes nodes for the started element are not preserved. Its skipped.
The current xslt looks like --

<xsl:template match="abc:configuration">
<xsl:copy>
<xsl:copy-of select="@*[local-name() != 'rmi' and local-name() != 'locale']"/>
<xsl:attribute name="useMgmt">
<xsl:text>true</xsl:text></xsl:attribute>
<xsl:attribute name="metadata"><xsl:text>true</xsl:text></xsl:attribute>
<xsl:apply-templates>
<xsl:sort select="boolean(self::server)" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:text></xsl:text>
</xsl:template>

<xsl:template match="server">
<xsl:element name="drillOutDir">
<xsl:value-of select="abc:configuration/drillOutDir"/>
</xsl:element>
<xsl:element name="started">
<xsl:copy-of select="@status[. = 'true' ]"/>
<xsl:value-of select="../abc:configuration/started"/>
</xsl:element>
</xsl:template>

<xsl:template match="abc:configuration/started">
<xsl:apply-templates/>
</xsl:template>

If I change this xslt to remove the started element from the server node and have a tranform for it separately then I do not even get the started node in the output xml.

What changes can I do to the above xslt in order to get
started node as in
<started status ="true"></started>
instead of what I m getting now ie
<started></started>

Thanks much.
[ February 21, 2005: Message edited by: N Bhonsle ]
N Bhonsle
Greenhorn

Joined: Jan 12, 2005
Posts: 13
this is resolved.

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Adding a new element to xml using xslt
 
Similar Threads
resolve namespace
XSLT needed
XSLT problem.
Namespace problem in xslt
XML Tranformation using XSLT