W3C home > Mailing lists > Public > xproc-dev@w3.org > April 2014

Re: try/catch with messages

From: Graham Seaman <graham@theseamans.net>
Date: Thu, 17 Apr 2014 10:16:58 +0100
Message-ID: <534F9C0A.2060506@theseamans.net>
To: xproc-dev@w3.org
Hi Gerrit

That works perfectly. I'd failed to understand (in spite of having read
the w3 doc!) that the error port is on the catch block and had been
trying to read the error from the group block instead, reasoning that
that was where the error would occur. Even if I'd corrected that
mistake, I don't think I would have realised that the catch block error
port is available within the catch block in spite of being an output and
not an input. So thank you very much for the working example.

Graham



On 17/04/14 06:56, Imsieke, Gerrit, le-tex wrote:
> Hi Graham,
> 
> There's a port called 'error' in the p:catch branch [1]. You need to
> forward this port to some other port that you declare both for the
> p:group and for the p:catch branch. In the example below, I added a
> 'status' port to the pipeline where there will either be an <ok/>
> document or the forwarded error port of p:catch. It's a bit verbose,
> admittedly, but that's how XProc is sometimes.
> 
> Gerrit
> 
> [1] http://www.w3.org/TR/xproc/#p.catch
> 
> 
> <p:declare-step
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>   xmlns:xs="http://www.w3.org/2001/XMLSchema"
>   xmlns:p="http://www.w3.org/ns/xproc"
>   version="1.0">       
>   <p:input port="source" primary="true">           
>     <p:inline>
>       <doc>Hello World!</doc>
>     </p:inline>
>   </p:input>
>   <p:output port="result" primary="true"/>
>   <p:output port="status">
>     <p:pipe port="status" step="try-test"/>
>   </p:output>
>   <p:option name="terminate" required="false" select="'yes'"/>
> 
>   <p:try name="try-test">
>     <p:group>
>       <p:output port="status">
>         <p:inline>
>           <ok/>
>         </p:inline>
>       </p:output>
>       <p:output port="result" primary="true"/>
>       <p:xslt name="tester">
>         <p:with-param name="terminate" select="$terminate" />
>         <p:input port="stylesheet">
>           <p:inline>
>             <xsl:stylesheet version="2.0">
>               <xsl:param name="terminate" as="xs:string"/>
>               <xsl:template match="/">
>                 <xsl:message terminate="{$terminate}">in XSLT step named
> 'tester'</xsl:message>
>                 <xsl:copy-of select="."/>
>               </xsl:template>
>             </xsl:stylesheet>
>           </p:inline>       
>         </p:input>
>         <p:input port="parameters">
>           <p:empty/>
>         </p:input>
>       </p:xslt>
>     </p:group>
>     <p:catch name="catch">
>       <p:output port="status">
>         <p:pipe port="result" step="forward-errors"/>
>       </p:output>
>       <p:output port="result" primary="true">
>         <p:inline>
>           <nodoc/>
>         </p:inline>
>       </p:output>
>       <p:identity name="forward-errors">
>         <p:input port="source">
>           <p:pipe port="error" step="catch"/>
>         </p:input>
>       </p:identity>
>       <p:sink/>
>     </p:catch>
>   </p:try>
> 
> </p:declare-step>
> 
> On 16.04.2014 17:27, Graham Seaman wrote:
>> Thanks Jonathan,
>>
>> That's a really helpful resource, and has cleared up a lot of my
>> questions about the operation of ports. Unfortunately the try/catch
>> examples generate an inline output in the catch sections, rather than
>> trying to access the error which made the try fail, as I'm trying to do.
>>
>> The following example now at least runs (using the same xml/xslt as
>> before). However, while a successful 'try' correctly outputs the xslt
>> transform output, when it fails because the xslt has terminated with an
>> xsl:message, the catch section simply reproduces the original input. I
>> still can't work out how to connect the catch input to the xslt error
>> message, instead of to the initial input.
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>>
>> <p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
>> version="1.0">       
>>     <p:input port="source" primary="true">           
>>         <p:document href="test.xml"/>
>>     </p:input>
>>     <p:output port="result" primary="true" sequence="false"/>
>>     
>>     <p:try name="try-test">
>>         <p:group>
>>             <p:xslt name="tester">
>>                 <p:input port="stylesheet">
>>                     <p:document href="test.xslt"/>       
>>                 </p:input>
>>                 <p:input port="parameters" sequence="true">
>>                     <p:empty/>
>>                 </p:input>
>>             </p:xslt>
>>         </p:group>
>>         <p:catch>
>>             <p:filter select="/"/>
>>         </p:catch>
>>     </p:try>
>>
>> </p:declare-step>
>>
>> Graham
>>
>> On 15/04/14 21:58, Cranford, Jonathan W. wrote:
>>> Graham,
>>>
>>> There's an example of try..catch embedded in the XProc tutorial at
>>> http://xfront.com/xproc/index.html, by Roger Costello and James Garriss.
>>>
>>> Look specifically at xproc/labs/lab09_try_catch in the zip file.
>>>
>>> This is a really good resource that was invaluable to me when first
>>> learning XProc.
>>>
>>> HTH,
>>>
>>> Jonathan Cranford
>>>
>>>
>>>> -----Original Message-----
>>>> From: Graham Seaman [mailto:graham@theseamans.net]
>>>> Sent: Tuesday, April 15, 2014 4:48 AM
>>>> To: xproc-dev@w3.org
>>>> Subject: try/catch with messages
>>>>
>>>> Hi,
>>>>
>>>> I'm in the 'initial 5 minutes' stage of learning xproc, and
>>>> floundering.
>>>> I have a series of xslt templates intended to validate files in
>>>> different ways. If validation fails at any point, the xslt terminates
>>>> with an error message (ie. using xsl;message with terminate='yes'). I'm
>>>> hoping to persuade xproc to catch and display the message produced in
>>>> the xslt, and would ideally like the xproc pipeline also to
>>>> terminate at
>>>> that point. However, I haven't even been able to get try/catch working
>>>> yet. Below is a (clearly non-working) attempt with some toy xml/xslt. I
>>>> think in general the biggest problem I'm having is with the scope of
>>>> the
>>>> various ports (for example, I don't understand why the global pipeline
>>>> source port is not visible to the p:xslt stage without adding a 'dummy'
>>>> parameter).
>>>>
>>>> Thanks for any advice
>>>>
>>>> Graham
>>>>
>>>>
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>
>>>> <p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="1.0">
>>>>     <p:input port="source" primary="true" sequence="false">
>>>>         <p:document href="test.xml"/>
>>>>     </p:input>
>>>>     <p:output port="result" primary="true" sequence="false"/>
>>>>
>>>>     <p:try name="try-test">
>>>>         <p:group>
>>>>             <p:xslt name="tester">
>>>>                 <p:input port="stylesheet">
>>>>                     <p:document href="test.xslt"/>
>>>>                 </p:input>
>>>>                 <p:with-param name="dummy" select="''"/>
>>>>             </p:xslt>
>>>>         </p:group>
>>>>         <p:catch>
>>>>             <p:identity>
>>>>                 <p:input port="source">
>>>>                     <p:pipe step="tester" port="error"/>
>>>>                 </p:input>
>>>>             </p:identity>
>>>>         </p:catch>
>>>>     </p:try>
>>>>
>>>>     <p:identity>
>>>>         <p:input port="source">
>>>>             <p:pipe step="try-test" port="result"/>
>>>>         </p:input>
>>>>     </p:identity>
>>>>
>>>> </p:declare-step>
>>>>
>>>> -------------------
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>
>>>> <words>
>>>>     <phrase>Hallo world</phrase>
>>>>     <phrase>and it's goodbye from me</phrase>
>>>> </words>
>>>>
>>>> -----------------------
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <xsl:stylesheet version="2.0"
>>>>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>>>>
>>>>     <xsl:template match="@* | node()">
>>>>         <xsl:copy>
>>>>             <xsl:apply-templates select="@*, node()"/>
>>>>         </xsl:copy>
>>>>     </xsl:template>
>>>>
>>>>     <xsl:template match="phrase">
>>>>         <xsl:if test="text() = 'and it''s goodbye from me'">
>>>>             <xsl:message terminate="yes">test.xml matched
>>>> phrase</xsl:message>
>>>>         </xsl:if>
>>>>         <xsl:copy-of select="."/>
>>>>     </xsl:template>
>>>> </xsl:stylesheet>
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
> 
Received on Thursday, 17 April 2014 10:12:00 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:03:12 UTC