Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

transform xml value but not attribute

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic