Jump to content

XSLT question, if node exists based on two child elements


dkaur

Recommended Posts

Following is my XML: I want to check if the node exists based on list_no and field. Display the vaue if exists and display '---' if does not.<Features><features_multiple_listings> <list_no>71150991</list_no> <value>Dishwasher, Range</value> <field>APPLIANCES</field> </features_multiple_listings><features_multiple_listings> <list_no>71150991</list_no> <value>Insulated</value> <field>WINDOWS</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Dishwasher, Range</value> <field>APPLIANCES</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Clubroom, Exercise Room, Exterior Maintenance, Water</value> <field>ASSC_FEE_INCLUDES</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Central Air</value> <field>COOLING</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Circuit Breakers</value> <field>ELECTRIC_FEATURE</field> </features_multiple_listings></Features>My XSL is the following and you can see it won't work as it is within for each loop. (for a given list_no, sometimes nodes exists with given feature and sometimes not, eg. clearly electric_feature is only availabe for 71149233 and not for 71150991) <xsl:for-each select="//Features/features_multiple_listings" <xsl:choose> <xsl:when test="field = 'ELECTRIC_FEATURE' and list_no = $v_listnumber"> <span class="ForcedLabelCenter"><xsl:value-of select="value"/></span> </xsl:when> <xsl:otherwise> <span class="ForcedLabelCenter">---</span> </xsl:otherwise> </xsl:choose> </xsl:for-each>

Link to comment
Share on other sites

Following is my XML: I want to check if the node exists based on list_no and field. Display the vaue if exists and display '---' if does not.<Features><features_multiple_listings> <list_no>71150991</list_no> <value>Dishwasher, Range</value> <field>APPLIANCES</field> </features_multiple_listings><features_multiple_listings> <list_no>71150991</list_no> <value>Insulated</value> <field>WINDOWS</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Dishwasher, Range</value> <field>APPLIANCES</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Clubroom, Exercise Room, Exterior Maintenance, Water</value> <field>ASSC_FEE_INCLUDES</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Central Air</value> <field>COOLING</field> </features_multiple_listings> <features_multiple_listings> <list_no>71149233</list_no> <value>Circuit Breakers</value> <field>ELECTRIC_FEATURE</field> </features_multiple_listings></Features>My XSL is the following and you can see it won't work as it is within for each loop. (for a given list_no, sometimes nodes exists with given feature and sometimes not, eg. clearly electric_feature is only availabe for 71149233 and not for 71150991) <xsl:for-each select="//Features/features_multiple_listings" <xsl:choose> <xsl:when test="field = 'ELECTRIC_FEATURE' and list_no = $v_listnumber"> <span class="ForcedLabelCenter"><xsl:value-of select="value"/></span> </xsl:when> <xsl:otherwise> <span class="ForcedLabelCenter">---</span> </xsl:otherwise> </xsl:choose> </xsl:for-each>
It works fine for me as long as you have your var "v_listnumbe" defined. You also forgot to close your for loop properly. If field isn't define, it'll return false.
    <xsl:for-each select="//Features/features_multiple_listings">      <xsl:choose>        <xsl:when test="field = 'ELECTRIC_FEATURE' and list_no = $v_listnumbe">          <span class="ForcedLabelCenter">            <xsl:value-of select="value"/>          </span>        </xsl:when>        <xsl:otherwise>          <span class="ForcedLabelCenter">---</span>        </xsl:otherwise>      </xsl:choose>    </xsl:for-each>

Link to comment
Share on other sites

Thanks for your reply.It will display "---" multiple times since it is in loop, whenever it does not match the condition..I am just finding a way around it...
I'm not really sure I understand what the problem is? Are you saying you don't want to show "---" if there is no match found? if that is the case just take the otherwise statement out.
      <xsl:choose>        <xsl:when test="field = 'ELECTRIC_FEATURE' and list_no = $v_listnumbe">          <span class="ForcedLabelCenter">            <xsl:value-of select="value"/>          </span>        </xsl:when>      </xsl:choose>    </xsl:for-each>

Link to comment
Share on other sites

I am sorry for not making it very clear. The following XSL will produce '------------------' instead of '---' and this is my problem. I want it to display only once if it is not found.<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:decimal-format name="staff" digit="D" /> <xsl:template match="/"> <xsl:variable name="v_listnumber"> <xsl:value-of select="71150991"/> </xsl:variable> <xsl:for-each select="//Features/features_multiple_listings"> <xsl:choose> <xsl:when test="field = 'ELECTRIC_FEATURE' and list_no = $v_listnumber"> <span class="ForcedLabelCenter"> <xsl:value-of select="value"/> </span> </xsl:when> <xsl:otherwise> <span class="ForcedLabelCenter">---</span> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

If only match is found in the whole list. I know that in my XML there is going to be distinct features per list_no so when match is found, displaying is not a problem. It is only when match is not found.Thank you for your response.

Link to comment
Share on other sites

Instead of the for-each use e.g.

<xsl:variable name="f" select="//Features/features_multiple_listings[field = 'ELECTRIC_FEATURE' and list_no = $v_listnumber]"/><xsl:choose>  <xsl:when test="$f">	   <span class="ForcedLabelCenter"><xsl:value-of select="$f/value"/></span>     <xsl:when>  <xsl:otherwise>---</xsl:otherwise></xsl:choose>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...