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"/></str ing>
</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></s truct>
<struct><var name="TITLE"><string>test 1yukkie!</string></var></s truct>
<struct><var name="TITLE"><string>test 1yukkie!</string></var></s truct>
</data>
************************** ********
However the xml looks like
************************** ********
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<test>test 1<anotherTest>yukkie!</ano therTest>< /test>
<test>test 1<anotherTest>yukkie!</ano therTest>< /test>
<test>test 1<anotherTest>yukkie!</ano therTest>< /test>
</root>
************************** ********
What I am really after is
************************** ********
<struct><var name="TITLE"><string>test 1
<struct><var name="TITLE"><string>yukki e!</string ></var></s truct>
</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">
**************************
<?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"/></str
</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></s
<struct><var name="TITLE"><string>test 1yukkie!</string></var></s
<struct><var name="TITLE"><string>test 1yukkie!</string></var></s
</data>
**************************
However the xml looks like
**************************
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<test>test 1<anotherTest>yukkie!</ano
<test>test 1<anotherTest>yukkie!</ano
<test>test 1<anotherTest>yukkie!</ano
</root>
**************************
What I am really after is
**************************
<struct><var name="TITLE"><string>test 1
<struct><var name="TITLE"><string>yukki
</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">
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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</de sc></opt>
<opt>a2<desc>blabla</desc> </opt>
<opt>b<desc>blabla</desc>< /opt>
<opt>b1<desc>blablabla</de sc></opt>
<opt>b2<desc>blabla</desc> </opt>
</xmlRoot>
much better, more logic:
<xmlRoot>
<opt>a<desc>blabla</desc>
<opt>a1<desc>blablabla</de sc></opt>
<opt>a2<desc>blabla</desc> </opt>
</opt>
<opt>b<desc>blabla</desc>
<opt>b1<desc>blablabla</de sc></opt>
<opt>b2<desc>blabla</desc> </opt>
</opt>
</xmlRoot>
example:
why got this:
<xmlRoot>
<opt>a<desc>blabla</desc><
<opt>a1<desc>blablabla</de
<opt>a2<desc>blabla</desc>
<opt>b<desc>blabla</desc><
<opt>b1<desc>blablabla</de
<opt>b2<desc>blabla</desc>
</xmlRoot>
much better, more logic:
<xmlRoot>
<opt>a<desc>blabla</desc>
<opt>a1<desc>blablabla</de
<opt>a2<desc>blabla</desc>
</opt>
<opt>b<desc>blabla</desc>
<opt>b1<desc>blablabla</de
<opt>b2<desc>blabla</desc>
</opt>
</xmlRoot>
<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