?
Solved

Check attribute exists using XSLT

Posted on 2007-10-15
3
Medium Priority
?
8,551 Views
Last Modified: 2013-11-18
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?
0
Comment
Question by:davesheppard
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 20079447
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
 

Author Comment

by:davesheppard
ID: 20084111
Nice one - thanks.  Looks like I was over complicating  things!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20084113
welcome
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

829 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