Solved

Ignore some elements in XSLT for-each loop

Posted on 2006-07-11
2
383 Views
Last Modified: 2013-11-19
I have an XML structure where one of the attributes of an element can be used more than once.  I want to have an XSLT that goes through each element and only picks out the first occurrences of the attribute in that element.

For instance,
Here is my XML:

<Node name="NodeA" type="Fixed" />
<Node name="NodeB" type="Fixed" />
<Node name="NodeC" type="Fixed" />
<Node name="NodeE" type="Pin" />
<Node name="NodeF" type="Fixed" />
<Node name="NodeG" type="Float" />
<Node name="NodeH" type="Float" />
<Node name="NodeI" type="Pin/>

And I want to use an XSLT to sort the elements and only pick out the first occurrences of each, therefore my resulting XML would be:

<Node name="NodeA" type="Fixed" />
<Node name="NodeE" type="Pin" />
<Node name="NodeG" type="Float" />

Unfortunately I can use a flag variable in an XSLT because I can't reset a variable.  That was the only way I could think of doing it.

<xsl:for-each select="//Node">
<xsl:sort select="@type" />
  <xsl:if ..the type is not the same as the previous... >
     ...Create the XML line here...
  </xsl:if>
     ...Otherwise ignore it...
</xsl:for-each>
0
Comment
Question by:tmar89
[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
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 17083146
Hi tmar89,
> <xsl:for-each select="//Node">

make it <xsl:for-each select="//Node[not(@type = preceding-sibling::Node/@type)]">
and you will only iterate over the unique ones


Cheers!
0
 

Author Comment

by:tmar89
ID: 17083251
Beautiful!
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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