set/test flag to format XML using XSL

I am processing an XML document and need to have an opening and closing string displayed according to a value of one of the XML tags.  I first check if Value = Title and then multiple Msg's will follow and finally in the first instance where I have EndTitle, I would like to put some text. I was wondering how to do something like this.

<xsl:variable name = "Title" >Title</xsl:variable>

<xsl:for-each select="Msg">

<xsl:variable name = "Status" ><xsl:value-of select="Status" /></xsl:variable>

<xsl:if test="$Status = $Title">
OPEN TITLE<br/>
<xsl:value-of select="MessageID" /><br/>

NEED TO SET FLAG HERE TO

</xsl:if>

<xsl:if test="$Status = $CloseTitle">
<xsl:if test="FLAG IS SET?">   I need to do something like this
CLOSE TITLE<br/>
</xsl:if>
</xsl:if>


The XML looks like this:

<Msg>
      <MessageNb>1</MessageNb>
      <MessageID>Message1</MessageID>
      <Message>This is message 1</Message>
      <Status/>
</Msg>
LVL 2
pesstAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
Hi pesst,

you can't set flags in XSLT,
so you need to deal with it using position() (to check the position in the for-each loop)
or with testing values in XPath

Cheers!
Gertone (Geert Bormans)Information ArchitectCommented:
pesst,
> the first instance where I have EndTitle

I am not sure the actual question is completely clear
where do you have endTitle? is itr a string value of MessageID?

cheers
pesstAuthor Commented:
It's in Status.
so i set $Status, to the current content of the XML tag "Status".

I need to have: if Title was found in the loop and if EndTitle is found later in another Msg then put some text.

thanks
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Gertone (Geert Bormans)Information ArchitectCommented:
pesst,

it could be something like this

<xsl:if test="$Status = $CloseTitle">
    <xsl:if test="preceding-sibling:Msg/Status = 'Title'">
    CLOSE TITLE<br/>
    </xsl:if>
</xsl:if>
Gertone (Geert Bormans)Information ArchitectCommented:
pesst,

The inner xsl:if tests whether there is a previous Msg with a Statusvalue 'Title'
if that is not what you need, changing is pretty straightforward
always here to help

cheers
pesstAuthor Commented:
Does <xsl:if test="preceding-sibling:Msg/Status = 'Title'"> if the previous tag was  'Title' or does it also work if 10 Msg's back it was 'Title'?
Gertone (Geert Bormans)Information ArchitectCommented:
it tries them all,
if you only need the previous one,
you need
<xsl:if test="preceding-sibling:Msg[1]/Status = 'Title'">
cheers
pesstAuthor Commented:
i think i will just change the XML to have a value of ClosedTitle and just check for that, instead of having formatting based on past tag values.
Gertone (Geert Bormans)Information ArchitectCommented:
from the modelling point of view that would be better

you could easily group them like this though

OPEN-TITLE
<xsl:for-each select="Msg[(Status = 'Title) or (Status = 'EndTitle') or (preceding-sibling::Msg/Status = 'Title' and following-sibling::Msg/Status = 'EndTitle')]">
     <xsl:value-of select="MessageID" /><br/>
</xsl:for-each>
CLOSE-TITLE

this way you can even have XML tags instead of just strings before and after
the for-each selects the EndTitle, Title and all Msgs in between

This is how I would do it without changing the XML

cheers

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.