Solved

XSLT: how to increment number value if attribute present

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iOS 10 iPad Zoom and acroll 4 53
text on slider doesn't resize nicely 5 38
Problem with a CSS in a page 2 26
FIELDSETs and LEGENDs in email markup 1 11
This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
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.
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

911 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

21 Experts available now in Live!

Get 1:1 Help Now