Solved

XSLT: how to increment number value if attribute present

Posted on 2016-11-01
4
25 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, html5 UI 1 49
spacing 5 32
How do I bind the results to a grid 3 24
microsoft access - xml 10 29
When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
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 to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

747 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

10 Experts available now in Live!

Get 1:1 Help Now