Solved

Traverse over children XSL

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML::LibXML and Xpath syntax How do I get attribute of sibling 2 147
Unattended XML settings 4 122
PHP and Soap 3 58
Online tools to transform xml to excel using xsl 1 48
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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