Link to home
Start Free TrialLog in
Avatar of Tacobell777
Tacobell777

asked on

Traverse over children XSL

I have an XSL transformation which works fine, but I need to traverse over all children as well and have no clue how to do that. The XSL looks like

**********************************
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<data>
<xsl:for-each select="/root/test">
<struct>
<var name="TITLE">
<string><xsl:value-of select="/root/test"/></string>
</var>
</struct>
</xsl:for-each>

</data>

</xsl:template>
</xsl:stylesheet>
**********************************

Which give me
**********************************
<?xml version="1.0" encoding="UTF-8"?>
<data>
<struct><var name="TITLE"><string>test 1yukkie!</string></var></struct>
<struct><var name="TITLE"><string>test 1yukkie!</string></var></struct>
<struct><var name="TITLE"><string>test 1yukkie!</string></var></struct>
</data>
**********************************

However the xml looks like

**********************************
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<test>test 1<anotherTest>yukkie!</anotherTest></test>
<test>test 1<anotherTest>yukkie!</anotherTest></test>
<test>test 1<anotherTest>yukkie!</anotherTest></test>
</root>
**********************************

What I am really after is

**********************************
<struct><var name="TITLE"><string>test 1
<struct><var name="TITLE"><string>yukkie!</string></var></struct>
</string></var></struct>
**********************************

In short I want to traverse through the tree and apply one style to all ellements. Then I'd also like to know how just traverse through them without knowing their names beforehand, and their name will become the <var name="Name here">
Avatar of rdcpro
rdcpro
Flag of United States of America image

This:

<xsl:value-of select="/root/test"/>

will always return the value of the same node, because you're using an absolute path.  Use a path relative to the context node, if you want a value that's related to the context node.


As far as what you're trying to do, I don't quite follow your question.  

If the XML looks like:

<root>
<test>test 1<anotherTest>Foo 1</anotherTest></test>
<test>test 2<anotherTest>Foo 2</anotherTest></test>
<test>test 3<anotherTest>Foo 3</anotherTest></test>
</root>

what is the desired output?

Regards,
Mike Sharp
ASKER CERTIFIED SOLUTION
Avatar of maverick65
maverick65

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alambres
alambres

does the xml structure depend on you? or is it like this and no way to change it? you really can do you're aiming with xslt, but if you want to nest nodes, the first  I would do (if you are allowed to) is get the nesting already in the xml. that's:

example:

why got this:

<xmlRoot>
  <opt>a<desc>blabla</desc></opt>
  <opt>a1<desc>blablabla</desc></opt>
  <opt>a2<desc>blabla</desc></opt>
  <opt>b<desc>blabla</desc></opt>
  <opt>b1<desc>blablabla</desc></opt>
  <opt>b2<desc>blabla</desc></opt>
</xmlRoot>

much better, more logic:
<xmlRoot>
  <opt>a<desc>blabla</desc>
     <opt>a1<desc>blablabla</desc></opt>
     <opt>a2<desc>blabla</desc></opt>
  </opt>
  <opt>b<desc>blabla</desc>
     <opt>b1<desc>blablabla</desc></opt>
     <opt>b2<desc>blabla</desc></opt>
  </opt>
</xmlRoot>