• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XSL transformation of child nodes

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an xml similar to this :

<?xml version="1.0" encoding="UTF-8"?>
<input:root xmlns:input="cross-field-validation-namespace">
<input:event>
<input:element>UserName.SWEUserName</input:element>
<input:elementtype>INPUT</input:elementtype>
<input:eventtype>deactivate</input:eventtype>
<input:value>LOGO</input:value>
</input:event>
<input:event>
...................................
..................................
</input:event>
..........................
.........................
</input:root>

I need to transform it to the following:
.......
<properties>
<property name="element" string_value="UserName.SWEUserName"/>
<property name="elementtype" string_value="INPUT" />
<property name="eventtype" string_value="deactivate" />
<property name="value" string_value="LOGO" />
</properties>
............

i.e all child nodes under the 'event' node get transformed to the attributes of the property node(node name as 'name' attribute and value as 'string_value').
Moreover , the number and name of child nodes under the different 'event' nodes can vary.

How can we write a XSL template to convert all chiild nodes under 'event' (however much there may be) into the desired format.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you can do most of this but you are just missing one thing? Then I think that one thing is the local-name() function. Give that a try.
 
Ranadhir Nag
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You were right.
With that things become easy.
The template looks like this - atleast from what I could figure out
<xsl:template match="input:root">
<xsl:apply-templates select="input:event" />
</xsl:template>

<xsl:template match="input:event">
<properties>
<xsl:apply-templates/>
</properties>
</xsl:template>

<xsl:template match="input:event/*">
<property name="{local-name()}" string_value="{.}"/>
</xsl:template>


But there is a small twist to this.Is there soem way to test whether a node has numeric value or not.A pseudocode will be like this :
if(test{.} is a number) then
<property name="{local-name()}" num_value="{.}"/>
else
<property name="{local-name()}" string_value="{.}"/>
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. Apply the number() function to the node, if it returns NaN then the value wasn't a number.

http://www.w3.org/TR/xpath#section-Number-Functions
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic