Thanks for the feedback. I'll take a look at the interaction of try/catch and validate. It doesn't surprise me greatly if there's a problem because validation works at some distance from the code that creates the element.
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Lewis Marshall
Sent: 10 May 2008 09:10
To: 'Mailing list for the SAXON XSLT and XQuery processor'
Subject: Re: [saxon] Validate result tree

Thanks for the reply.

Yes, I see the sense of that.

I have tried created a temp doc with the parent and the current test child but the try/catch doesn’t catch, it returns a validation error and the transform stop.

 

      <xsl:variable name="chk"

                    select="saxon:try( xslf:tryInsert( $parent, $nodename ), saxon:function( 'xslf:error', 1 ) )"/>

 

  <xsl:function name="xslf:tryInsert">

    <xsl:param name="parent"/>

    <xsl:param name="nodename"/>

    <xsl:document validation="strict">

      <xsl:element name="{$parent}" validation="strict">

        <xsl:element name="{$nodename}"/>

      </xsl:element>

    </xsl:document>

  </xsl:function>

 

So, whatever I’m doing wrong means I still can’t catch the error and try doing something else.

 

Thanks, Lewis Marshall

 


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Michael Kay
Sent: 09 May 2008 15:38
To: 'Mailing list for the SAXON XSLT and XQuery processor'
Subject: Re: [saxon] Validate result tree

 

When you validate an element, you are checking that the children of the element are valid against the content model of that element. So if you want to check whether a particular child can legitimately appear in a particular place, you need to be validating its parent. Saxon will detect an error as soon as you try to insert in inappropriate child, but the error is on the expression that constructs the parent element (the element being validated), not the expression that constructs the child. Having said that, it's quite possible that a try/catch around the expression that creates the inappropriate child will catch the error - in theory it shouldn't, but I would need to test it to be sure.

 

Validating the child doesn't catch any error because the child is valid, as defined in XML Schema. It's the parent that is invalid.

 

Michael Kay

http://www.saxonica.com/

 


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Lewis Marshall
Sent: 09 May 2008 14:53
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Validate result tree

Hi

 

I am investigating the feasibility of creating a schema-aware XSLT that continually check validation of the result tree to test for the correct insertion of an element.

 

For example, I have a recursive template that acts like a glorified ‘copy-of’. When I try to insert the element into the result tree I try to calculate the correct node name then test it in the tree.

 

At the moment all I am able to do is build up the tree, element by element, then create a variable for the current element and use a copy-of with a saxon:try to test if the result will be valid. The test returns true if it finds a global declaration in my schema, not if it is valid to insert a node with that name in to the result tree.

 

In my simplified example below, my if( saxon:try( $output, saxon:function( 'xslf:error', 1 ) ) ) then '' else '' returns true | false for the insertion of my $output in isolation, not as part of the result tree.

 

Any clue how I can check against the whole result tree?

 

  <xsl:template name="create-nodeset">

 

    <xsl:param name="ns"/>

 

    <xsl:for-each select="$ns">

 

      <xsl:variable name="nodename">

        <xsl:choose>

          <xsl:when test="self::section and $trail='document'">customnodename</xsl:when>

          <xsl:otherwise><xsl:value-of select="name(.)"/></xsl:otherwise>

        </xsl:choose>

      </xsl:variable>

 

      <saxon:assign name="glbNodeChk" select="1"/>

      <saxon:assign name="glbValdMsg"/>

 

      <xsl:variable name="output">

        <xsl:element name="{$nodename}" validation="strict"/>

      </xsl:variable>

 

      <xsl:copy-of select="if( saxon:try( $output, saxon:function( 'xslf:error', 1 ) ) ) then ''  else ''"/>

 

      <xsl:if test="$glbNodeChk=1">

 

        <xsl:element name="{$nodename}">

 

          <!-- <xsl:copy-of select="@*"/> -->

 

          <xsl:for-each select="child::* | processing-instruction() | text()">

            <xsl:choose>

 

              <xsl:when test="self::processing-instruction()"><xsl:copy-of select="."/></xsl:when>

 

              <xsl:when test="self::text()"><xsl:value-of select="translate( .,'&#x0A;',' ')"/></xsl:when>

 

              <xsl:when test="self::node()">

                <xsl:call-template name="create-nodeset">

                  <xsl:with-param name="ns" select="current()"/>

                </xsl:call-template>

              </xsl:when>

 

            </xsl:choose>

          </xsl:for-each>

        </xsl:element>

      </xsl:if>

    </xsl:for-each>

  </xsl:template>

 

  <xsl:function name="xslf:error">

    <xsl:param name="error"/>

    <saxon:assign name="glbNodeChk" select="0"/>

    <saxon:assign name="glbValdMsg" select="$error"/>

  </xsl:function>

 

 

Many thanks, Lewis Marshall