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?
 
Geert BormansInformation 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
0
 
Geert BormansInformation 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!
0
 
Geert BormansInformation 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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
0
 
Geert BormansInformation 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>
0
 
Geert BormansInformation 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
0
 
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'?
0
 
Geert BormansInformation ArchitectCommented:
it tries them all,
if you only need the previous one,
you need
<xsl:if test="preceding-sibling:Msg[1]/Status = 'Title'">
cheers
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.