Solved

Traverse over children XSL

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
xml read and write 1 56
SVG, D3, scaling, translation 11 126
Excel VBA and XML - traversing the nodes 2 56
c# code 19 56
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now