Solved

Ignore some elements in XSLT for-each loop

Posted on 2006-07-11
2
359 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

770 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