Check attribute exists using XSLT

Hiya

I've got an XML file which looks as follows:

<PolicyExtension>
  <bCharged>false</bCharged>
  <lID>5</lID>
  <sDesc>text</sDesc>
</PolicyExtension>
<PolicyExtension>
  <bCharged>false</bCharged>
  <lID>9</lID>  
  <sDesc>text</sDesc>
</PolicyExtension>
<PolicyExtension>
  <bCharged>false</bCharged>
  <lID>3</lID>
  <sDesc>text</sDesc>
</PolicyExtension>
<PolicyExtension>
  <bCharged>false</bCharged>
  <lID>2</lID>
  <sDesc>text</sDesc>
</PolicyExtension>

If an attribute exists with a lID of 9 then I need to write <additional_windscreen_cover>Y</additional_windscreen_cover> from within an XSLT script.

If this attribute does not exist then I need to write <additional_windscreen_cover>N</additional_windscreen_cover>

So far, my code looks like:

<xsl:for-each select="/PoliciesAtDate/PolicyAtDate/Policy/PolicyExtensions/PolicyExtension">
  <xsl:choose>
    <xsl:when test="lID = 9">
      <additional_windscreen_cover>Y</additional_windscreen_cover>
    </xsl:when>            
    <xsl:otherwise>
      <additional_windscreen_cover>N</additional_windscreen_cover>
    </xsl:otherwise>      
  </xsl:choose>                                    
</xsl:for-each>

This outputs

<additional_windscreen_cover>N</additional_windscreen_cover>
<additional_windscreen_cover>Y</additional_windscreen_cover>
<additional_windscreen_cover>N</additional_windscreen_cover>
<additional_windscreen_cover>N</additional_windscreen_cover>

because it obviously writes out the XML for each PolicyExtension it reads.  I only need it to output Y or N depending on whether lID 9 exists or not.

I did also try the following code as using variables seemed like the most obvious solution:

<xsl:variable name="sWindscreen">N</xsl:variable>
<xsl:for-each select="/PoliciesAtDate/PolicyAtDate/Policy/PolicyExtensions/PolicyExtension">
  <xsl:choose>
    <xsl:when test="lID = 9">                                                                              
      <xsl:variable name="sWindscreen">Y</xsl:variable>                  
    </xsl:when>                  
  </xsl:choose>                                    
</xsl:for-each>
<additional_windscreen_cover><xsl:value-of select="$sWindscreen"/></additional_windscreen_cover>

But, this gives an error that sWindscreen cannot be defined twice within the same template.

Please can anyone help?
davesheppardAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
remove the for-each from the test
and use
                    <xsl:choose>
                        <xsl:when test="/PoliciesAtDate/PolicyAtDate/Policy/PolicyExtensions/PolicyExtension/lID = 9">
                            <additional_windscreen_cover>Y</additional_windscreen_cover>
                        </xsl:when>            
                        <xsl:otherwise>
                            <additional_windscreen_cover>N</additional_windscreen_cover>
                        </xsl:otherwise>      
                    </xsl:choose>                                    

it will then only be printed once
0
 
davesheppardAuthor Commented:
Nice one - thanks.  Looks like I was over complicating  things!
0
 
Geert BormansInformation ArchitectCommented:
welcome
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.