Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XSLT: how to increment number value if attribute present

Posted on 2016-11-01
4
Medium Priority
?
97 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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

877 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