Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Traverse over children XSL

Posted on 2004-04-06
3
Medium Priority
?
1,323 Views
Last Modified: 2006-11-17
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">
0
Comment
Question by:Tacobell777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 10770878
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
0
 
LVL 4

Accepted Solution

by:
maverick65 earned 2000 total points
ID: 10771152
I think you wanna have this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
       <xsl:template match="/root">
                <data>
                      <!-- for all childs of root, whatever its name is -->
                      <xsl:apply-templates />
                </data>
      </xsl:template>
      
      
      <!-- template for all nodes which have no special rule -->
      <xsl:template match="*">
            <struct>
                  <var>
                        <!-- add the name attribute to the var tag -->
                        <xsl:attribute name="name">
                              <xsl:value-of select="name()"/>
                        </xsl:attribute>
                        <string>
                              <!-- further processing for all childs -->
                              <xsl:apply-templates/>
                        </string>
                  </var>
            </struct>
      </xsl:template>
      
      <!-- all tags with name test2 are differently processed -->
      <xsl:template match="test2">
            <test2>this is test 2</test2>
      </xsl:template>
</xsl:stylesheet>
0
 
LVL 5

Expert Comment

by:alambres
ID: 10772775
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>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question