This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes transform xml value but not attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "transform xml value but not attribute" Watch "transform xml value but not attribute" New topic
Author

transform xml value but not attribute

Greg Schultz
Greenhorn

Joined: Oct 08, 2003
Posts: 16
I am very new to XSLT and having a difficult time in transforming some. I have an XML input stream that contains

<xj:CODAmount currency="USD">0.0</xj:CODAmount>

I need to transform this into

<xj:CODAmount currency="USD">
<xj:amount>0.0</xj:amount>
</xj:CODAmount>


Does anyone have an easy way to do this. I have initially tried

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nj="http://www.xj.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl utput method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="no" />
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="*|@*"/>
<xsl:if test="text()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

but the result is ending up like this:

<xj:CODAmount>USD0.0</xj:CODAmount>


TIA,
Greg
Dmitry Kirsanov
Author
Ranch Hand

Joined: Apr 26, 2004
Posts: 33
Try something like:

<xsl:template match="xj:CODAmount">
<xj:CODAmount>
<xsl:copy-of select="@*"/> <!-- copy attrs -->
<xj:amount> <!-- wrap text in xj:amount -->
<xsl:apply-templates select="text()"/>
</xj:amount>
</xj:CODAmount>
</xsl:template>
Greg Schultz
Greenhorn

Joined: Oct 08, 2003
Posts: 16
Thanks Dmitry, that worked great. There are a couple of more issues, if you don't mind.

I added this to get all other nodes.

<xsl:template match="*">
<xsl:copy-of select="@*"/>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

It works fine except for a couple of tags.

A tag with attributes but no elements,

<nj:Version major="6.0" minor="0.0"/>

becomes

<nj:Version/>

And this section, where dimension has one attribute and three elements,

<nj ackageDimensions>
<nj:EstimatedDimensions UOM="inch">
<nj:Height>55</nj:Height>
<nj:Length>55</nj:Length>
<nj:Width>55</nj:Width>
</nj:EstimatedDimensions>
<nj:ActualDimensions UOM="inch">
<nj:Height>53</nj:Height>
<nj:Length>53</nj:Length>
<nj:Width>49</nj:Width>
</nj:ActualDimensions>
</nj ackageDimensions>

becomes

<nj ackageDimensions UOM="inch">
<nj:EstimatedDimensions>
<nj:Height>55</nj:Height>
<nj:Length>55</nj:Length>
<nj:Width>55</nj:Width>
</nj:EstimatedDimensions>
<nj:ActualDimensions>
<nj:Height>53</nj:Height>
<nj:Length>53</nj:Length>
<nj:Width>49</nj:Width>
</nj:ActualDimensions>
</nj ackageDimensions>


Any ideas?

Thanks very much for your help,

Greg
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: transform xml value but not attribute
 
Similar Threads
Can Not XSL-Transform Something so Simple.
Extracting XSL content to XML with XSLT problem ...
to EXACT Dublicate an xml file[xml->xml] via Java TransformerFactory classes and XSTL well I do not
inserting data from XML to database
Namespace problem in xslt