Solved

XSLT: how to increment number value if attribute present

Posted on 2016-11-01
4
47 Views
Last Modified: 2016-11-02
i have the following input

<xml>
      <Flight ID="F1">
      <Flight ID="F2">
      <Flight ID="F3" AddNew="Yes">
      <Flight ID="F4">
</xml>
      
      

I want my output to read:

<xml>
       <Flight ID="1"/>
      <Flight ID="2"/>
      <Flight ID="4"/>
      <Flight ID="5"/>
</xml>      



I have following xsl .which loops through each @ID and builds the ID by grabbing number after 'F'. And this is ok but I want to increment the number if AddNew="Yes" exists.  



<xsl:variable name="Seg" select="//Flight/@ID"/>


<xsl:for-each select="$Seg">

<xsl:variable name="ID">
  <xsl:value-of select="substring-after(current(), 'F')"/>
</xsl:variable>


<xsl:attribute name="ID" >
<xsl:value-of select="$ID"/>
</xsl:attribute>

.....
0
Comment
Question by:badtz7229
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 41869215
Try this

    <xsl:template match="xml">
        <xml>
            <xsl:for-each select="//Flight">
                <Flight>
                    <xsl:attribute name="ID" >
                        <xsl:value-of select="substring-after(@ID, 'F') + count(self::Flight[@AddNew = 'Yes']) + count(preceding-sibling::Flight[@AddNew = 'Yes'])"/>
                    </xsl:attribute>
                </Flight>
            </xsl:for-each>
        </xml>
    </xsl:template>

Open in new window

0
 

Author Comment

by:badtz7229
ID: 41870226
@Geert Bormans
Yes that worked fine.
can you explain  count(preceding-sibling::Flight[@AddNew = 'Yes']) ?
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 500 total points
ID: 41870267
count() counts the number of nodes found

preceding-sibling::Flight are preceding siblings with name Flight, a sibling being a parent of the same child, preceding one that comes earlier
<Flight ID="F1"/> and <Flight ID="F2"/> are the preceding siblings of <Flight ID="F3" AddNew="Yes"/>

[@AddNew = 'Yes'] restricts the found nodeset to those that have an attribute AddNew = 'Yes'

Basically, since you increment one on each occurence of AddNew =  Yes, I count with this statement how many Flights have passed with the attribute set and add that number to the original

at <Flight ID="F4"> we have "4" and we add "1" since we have one  count(preceding-sibling::Flight[@AddNew = 'Yes'])  (<Flight ID="F3" AddNew="Yes">) so we render <Flight ID="5"/>
0
 

Author Closing Comment

by:badtz7229
ID: 41870337
thank you for your help.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML response optional elements 12 50
Botom of page is wrong color 5 17
ASP.NET Content Page 3 29
C# Formatting DateTime String Value 5 31
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…

837 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