Solved

Traverse over children XSL

Posted on 2004-04-06
3
1,313 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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