XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] XSTL stylesheet workarounds for exceptions

Hi Michael,

 

Thanks for your frank constructive advice and I have worked out where the problem lies after some more reading.

Regards,

Jack




From: Michael Kay <mike@saxonica.com>
To: Jack Bush <netbeansfan@yahoo.com.au>; xml-dev@lists.xml.org
Sent: Monday, 9 February, 2009 9:35:21 PM
Subject: RE: [xml-dev] XSTL stylesheet workarounds for exceptions

I think there's a problem with the approach you are using to learning the language. You're basically guessing, and you're guessing wrong. Where did you get the idea of using curly braces in ${no_hobbie} = 'false'? Probably from some previous language you have used in which curly braces are used in such a context. That's an understandable mistake. But when the compiler then tells you
 
XPST0003: XPath syntax error at char 1 on line 35 in {${no_hobbies}:

    expected "<name>", found "{"

 
surely the right thing to do is to look up in your favourite XPath reference book or tutorial what the correct syntax for a variable reference is? That can't be hard.
 
On the other hand, writing
 
        <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>

 

is a common mistake made by people transferring from other languages, and it's understandable that you should be confused by the error message. But if you did some reading, I'm sure the introductory chapters of any XSLT book would explain that variables in XSLT aren't like the variables of a procedural language: you can't assign to them at arbitrary points in your processing logic.

 

Several of your code examples use instructions like xsl:if outside the context of a template, or put templates in the middle of an instruction. This shows that you haven't yet grasped the basic structure of a stylesheet, which consists of a set of templates and functions each containing instructions. I don't think that it's going to be helpful to give you more coding snippets until you have understood the basic concepts, and for that I really think you would be well advised to do some reading.

 

Michael Kay

http://www.saxonica.com/



From: Jack Bush [mailto:netbeansfan@yahoo.com.au]
Sent: 09 February 2009 10:18
To: xml-dev@lists.xml.org
Subject: [xml-dev] XSTL stylesheet workarounds for exceptions

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.


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

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS