• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

transform xml value but not attribute

 
Greg Schultz
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic