Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSLT: how to increment number value if attribute present

Posted on 2016-11-01
4
Medium Priority
?
79 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
[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
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
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…
Suggested Courses

704 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