aspose file tools*
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: transform xml value but not attribute