Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xml-dev] XSTL stylesheet workarounds for exceptions

From: Jack Bush <netbeansfan@-----.---.-->
To: xml-dev@-----.---.---
Date: 2/9/2009 10:19:00 AM
Hi All,
 
I am unable to come up with a suitable workaround to kick in when an element is not available. Below are various approaches attempted without success:
 
Scenario   
A. <xsl:template match="/">
  <area>
    <xsl:apply-templates select="//ns:p"/>
      <xsl:variable name="hobbie" select="ns:p[ns:strong='Hobbie:']"/>
      <xsl:apply-templates select="$hobbie"/>
      <xsl:choose>
        <xsl:when test="empty($hobbie)">
          <xsl:variable name="no_hobbie" select="true"/>  
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="no_hobbie" select="false"/>
        </xsl:otherwise>
      </xsl:choose>
  </area>
</xsl:template>
 
<xsl:if test="${no_hobbie} = 'false'">
  <xsl:template match="ns:p[ns:strong='Hobbie:']">
    <xsl:for-each select="text()[normalize-space() != '']"> 
      <hobbie><xsl:value-of select="normalize-space()"/></hobbie> 
    </xsl:for-each> 
  </xsl:template> 
</xsl:if>
  
Compilation error Error at xsl:if on line 35 column 41 
  XPST0003: XPath syntax error at char 1 on line 35 in {${no_hobbies}:
    expected "<name>", found "{"
Error at xsl:if on line 35 column 41 
  XTSE0010: Element xsl:if must not appear directly within xsl:stylesheet
Warning: at xsl:variable on line 13 column 59 
  SXWN9001: A variable with no following sibling instructions has no effect
Warning: at xsl:variable on line 16 column 60 
  SXWN9001: A variable with no following sibling instructions has no effect
Error at xsl:template on line 36 column 53 
  XTSE0010: An xsl:if element must not contain an xsl:template element
Error at xsl:template on line 36 column 53 
  XTSE0010: Element must be used only at top level of stylesheet 
B. <xsl:template match="/">
  <area>
    <xsl:apply-templates select="//ns:p"/>
      <xsl:variable name="hobbie" select="ns:p[ns:strong='Hobbie:']"/>
      <xsl:apply-templates select="$hobbie"/>
      <xsl:choose>
        <xsl:when test="empty($hobbie)">
          <hobbie>Unknown</hobbie>  
        </xsl:when>
        <xsl:otherwise>
          <xsl:template match="ns:p[ns:strong='Hobbie:']"> 
            <xsl:for-each select="text()[normalize-space() != '']"> 
              <hobbie><xsl:value-of select="normalize-space()"/></hobbie> 
            </xsl:for-each> 
          </xsl:template>
        </xsl:otherwise>
      </xsl:choose>
  </area>
</xsl:template>
  
Compilation error Error at xsl:template on line 17 column 61 
  XTSE0010: An xsl:otherwise element must not contain an xsl:template element
Error at xsl:template on line 17 column 61 
  XTSE0010: Element must be used only at top level of stylesheet 
C. <xsl:template match="/">
  <area>
    <xsl:apply-templates select="//ns:p"/>
      <xsl:variable name="hobbie" select="ns:p[ns:strong='Hobbie:']"/>
      <xsl:apply-templates select="$hobbie"/>
      <xsl:choose>
        <xsl:when test="empty($hobbie)">
          <hobbie>Unknown</hobbie>  
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="hobbie"/>
        </xsl:otherwise>
      </xsl:choose>
  </area>
</xsl:template>
 
<xsl:template name="hobbie" match="ns:p[ns:strong='Hobbie:']">
  <xsl:for-each select="text()[normalize-space() != '']"> 
    <hobbie><xsl:value-of select="normalize-space()"/></hobbie> 
  </xsl:for-each> 
</xsl:template>
  
Output <hobbie>fishing</hobbie>      // should pick up the valid element if there is one exist.
<hobbie>Unknown</hobbie>  // only create a default element value when no such element exist.  
D. <xsl:template match="/">
  <area>
    <xsl:apply-templates select="//ns:p"/>
      <xsl:variable name="hobbie" select="ns:p[ns:strong='Hobbie:']"/>
      <xsl:apply-templates select="$hobbie"/>
      <xsl:if test="empty($hobbie)">
          <hobbie>Unknown</hobbie>  
      </xsl:if>
  </area>
</xsl:template>
 
<xsl:template match="ns:p[ns:strong='Hobbie:']">
  <xsl:for-each select="text()[normalize-space() != '']"> 
    <hobbie><xsl:value-of select="normalize-space()"/></hobbie> 
  </xsl:for-each> 
</xsl:template>
  
Output <hobbie>fishing</hobbie>      // should pick up the valid element if there is one exist.
<hobbie>Unknown</hobbie>  // only create a default element value when no such element exist. 
 
I am very new XSLT and can do with some helpful suggestion.
 
Many thanks,
Jack


      Make Yahoo!7 your homepage and win a trip to the Quiksilver Pro. Find out more


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent