Results 1 to 8 of 8

Thread: Node searching in an xsl:if test

  1. #1
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    758

    Node searching in an xsl:if test


    Here's a snippet of the xml in question:
    Code:
    <FACILITY_RELATED>
      <PES_ID>384</PES_ID> 
      <ES_ID>402</ES_ID> 
      <RELATED_CODE>H</RELATED_CODE> 
      <AUTO_RELATED>false</AUTO_RELATED> 
      <PESRelatedFacilityNumber>1039</PESRelatedFacilityNumber> 
      <PESRelatedFacilityTypeCode>IHB</PESRelatedFacilityTypeCode> 
      <ESRelatedFacilityNumber>1010</ESRelatedFacilityNumber> 
      <ESRelatedFacilityTypeCode>AGM</ESRelatedFacilityTypeCode> 
    </FACILITY_RELATED>
    <FACILITY_RELATED>
      <PES_ID>384</PES_ID> 
      <ES_ID>401</ES_ID> 
      <RELATED_CODE>Q</RELATED_CODE> 
      <AUTO_RELATED>false</AUTO_RELATED> 
      <PESRelatedFacilityNumber>1038</PESRelatedFacilityNumber> 
      <PESRelatedFacilityTypeCode>IHB</PESRelatedFacilityTypeCode> 
      <ESRelatedFacilityNumber>1010</ESRelatedFacilityNumber> 
      <ESRelatedFacilityTypeCode>AGM</ESRelatedFacilityTypeCode> 
    </FACILITY_RELATED>
    All these FACILITY_RELATED nodes are records from a dataset. What I need to do is determine if a given pes_id is present, and if so, then do something. I don't have to aquire the node or even know how many nodes are present - just know if it's there.

    But how do I do this in an xsl:if? I basically want to be able to say <xsl:if test="if there's a pes_id = 5 somewhere in the facility_related nodes, then do something" />

    I thought I would have the test in an xml:template - something like:
    Code:
        <xsl:template name ='IsIDPresent'><xsl:param name='ID' />
            <xsl:for-each select ='//FACILITY_RELATED'>
                <xsl:if test ='PES_ID = $ID'>
                    <xsl:value-of select ='PES_ID'/>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    But I couldn't figure out how to put the call to the template into the xsl:if test.

    Are there other ways to do what I'm trying to do? Any help would be appreciated.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  2. #2
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,479

    Re: Node searching in an xsl:if test

    There is no need to loop through the nodes if all you are checking is the value exists somewhere in the document. Something like this should work.
    Code:
      <xsl:template match="/">
        <xsl:call-template name='IsIDPresent'>
          <xsl:with-param name='ID'>384</xsl:with-param>
        </xsl:call-template> 
      </xsl:template>
    
    
      <xsl:template name ='IsIDPresent'>
        <xsl:param name='ID' />
        <xsl:if test ='//FACILITY_RELATED/PES_ID = $ID'>
          <xsl:value-of select ='$ID'/> - present
        </xsl:if>
      </xsl:template>

  3. #3
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    758

    Re: Node searching in an xsl:if test

    Thanks again. I can't find any references online to any kind of complex xsl. Everything tutorial I find is so simplistic.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  4. #4
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    758

    Re: Node searching in an xsl:if test

    One other question: How would I evaluate the call to the template in an xsl:if statement? In other words, how would I say "if <xsl:call-template name='IsIDPresent'>, then (execute some more xsl)"
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  5. #5
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,479

    Re: Node searching in an xsl:if test

    Change your template to use choose instead of an if. You can use multiple when tests inside a choose and the otherwise would be the else part of an if/else if/else statement
    Code:
      <xsl:template name ='IsIDPresent'>
        <xsl:param name='ID' />
        <xsl:choose>
          <xsl:when test ='//FACILITY_RELATED/PES_ID = $ID'>
            <xsl:value-of select ='$ID'/> - present
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select ='$ID'/> - not present
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

  6. #6
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    758

    Re: Node searching in an xsl:if test

    Maybe I'm thinking of this wrong. I'm thinking of the 'IsIDPresent' template sort of like a function that returns a boolean - or something that can be interpreted like a boolean. My plan was to call the template from another place in the code and then, based upon the value returned, either execute a block of code or skip it. But maybe I'm misunderstanding templates. (noobishness).
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  7. #7
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,479

    Re: Node searching in an xsl:if test

    Is this closer to your needs?

    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
      <xsl:template match="/">
        <xsl:variable name="blnPresent">
          <xsl:call-template name='IsIDPresent'>
            <xsl:with-param name='ID'>384</xsl:with-param>
          </xsl:call-template>
        </xsl:variable>
    
        <xsl:choose>
          <xsl:when test='$blnPresent="true"'>Value found</xsl:when>
          <xsl:otherwise>Value not found</xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    
      <xsl:template name ='IsIDPresent'>
        <xsl:param name='ID' />
        <xsl:if test ='//FACILITY_RELATED/PES_ID = $ID'>true</xsl:if>
      </xsl:template>
      
    </xsl:stylesheet>
    if it is you could do the samething without a template
    Code:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
      <xsl:template match="/">
        <xsl:variable name="blnPresent">
          <xsl:if test ='//FACILITY_RELATED/PES_ID = 385'>true</xsl:if>
        </xsl:variable>
    
        <xsl:choose>
          <xsl:when test='$blnPresent="true"'>Value found</xsl:when>
          <xsl:otherwise>Value not found</xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>

  8. #8
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    758

    Re: Node searching in an xsl:if test

    Yes, now it's starting to make sense.

    Thanks again.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •