Topic
  • 9 replies
  • Latest Post - ‏2012-10-24T14:03:08Z by SystemAdmin
SystemAdmin
SystemAdmin
746 Posts

Pinned topic Small complex lookups sprinkled arbitrarily through long file being XSLT'd

‏2012-10-17T19:47:19Z |
I am looking for a suggestion for a general approach to what I want to achieve. I am new to XQuery, but did write one XSLT stylesheets several years ago.

It might be easier to describe the desired output, and go backwards from there. The output is a text or xhtml document (e.g., a journal article) that contains properly formatted citations. A citation to a given source may be relatively complicated, being dependent on the type of document, and where the citation appears in the document. One might imagine that in an document being input for formatting, a citation would appear as a function call. The function call might have parameters, including an identifier for the reference, and optionally: page numbers, context indicators, and/or parenthetical strings.

Suppose the full citations to which the citations refer are stored in XML, and that a given full citation might itself be rather complicated. Basically, the citation function call is supposed to (1) choose the right full citation from the list or database, (2) select and sequence appropriate bits and pieces from the full version of the citation, by interpreting the parameters of the function call; and (3) fully format the citation for insertion into the document.

XQuery seems to be the natural approach for implementing such a function. XSLT seems to be the natural approach for doing the overall transformation and invoking the function calls as they come up.

Based on my limited knowledge of the two languages, it appears that XQuery functions cannot be called from within an XSLT stylesheet. If that is correct, then what is the best alternative approach? Thanks.
Updated on 2012-10-24T14:03:08Z at 2012-10-24T14:03:08Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-17T20:45:39Z  
    One approach might be to call a Java function from XSLT, and have the Java function call XQuery via XQJ.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-17T22:44:54Z  
    There are a number of products that offer XSLT and XQuery in one package (Saxon is an example) and many of these (Saxon is an example) offer a way to call XQuery functions from XSLT.

    However, there's nothing in the description you have given that suggests writing an XQuery function will be significantly easier than writing an XSLT 2.0 function, if the overall logic of the application is in XSLT.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-18T00:11:14Z  
    There are a number of products that offer XSLT and XQuery in one package (Saxon is an example) and many of these (Saxon is an example) offer a way to call XQuery functions from XSLT.

    However, there's nothing in the description you have given that suggests writing an XQuery function will be significantly easier than writing an XSLT 2.0 function, if the overall logic of the application is in XSLT.
    Dear Dr. Kay:

    Thank you for your reply. I could not have written that one style sheet (actually, I think there were two or three, but one of them has been applied regularly ever since) without your fantastic book. Your explanations were so clear that I even followed the chess example. I just bought a copy of the 4th edition. You also helped me individually through email correspondence with something I couldn't figure out, way back then.

    I am an XQuery newbie, and have forgotten nearly everything I ever learned about XSLT. But here is why I think XQuery is warranted. Going from a few hints about the citation, as would be provided to a function, to a citation correctly formatted in context involves lots of special rules, see https://www.legalbluebook.com/, and therefore plenty of logical decisions. The simplified syntax of XQuery seems like it would be more appropriate for this.

    I bought a copy of Oxygen. Does the version of Saxon embedded in that tool support calling XQuery from XSLT? Could you give me a hint about where this is discussed in your book? Thanks!
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-18T08:35:45Z  
    Dear Dr. Kay:

    Thank you for your reply. I could not have written that one style sheet (actually, I think there were two or three, but one of them has been applied regularly ever since) without your fantastic book. Your explanations were so clear that I even followed the chess example. I just bought a copy of the 4th edition. You also helped me individually through email correspondence with something I couldn't figure out, way back then.

    I am an XQuery newbie, and have forgotten nearly everything I ever learned about XSLT. But here is why I think XQuery is warranted. Going from a few hints about the citation, as would be provided to a function, to a citation correctly formatted in context involves lots of special rules, see https://www.legalbluebook.com/, and therefore plenty of logical decisions. The simplified syntax of XQuery seems like it would be more appropriate for this.

    I bought a copy of Oxygen. Does the version of Saxon embedded in that tool support calling XQuery from XSLT? Could you give me a hint about where this is discussed in your book? Thanks!
    Information on how to call XQuery functions from XSLT in Saxon can be found here:

    http://www.saxonica.com/documentation9.4-demo/index.html#!extensions/instructions/import-query

    Like all Saxon extensions to the specs, you need at least the Professional Edition for this to work.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-24T01:02:57Z  
    Information on how to call XQuery functions from XSLT in Saxon can be found here:

    http://www.saxonica.com/documentation9.4-demo/index.html#!extensions/instructions/import-query

    Like all Saxon extensions to the specs, you need at least the Professional Edition for this to work.
    As I said, I am rusty with XSLT. But at this point, my goal is limited--I just want to get the simplest import to work. This is run within Oxygen Editor 14.1. Comments appreciated.

    Here is what I tried, which resulted in the following two errors:

    
    Engine name: Saxon6.5.5 Severity: error Description: Failed to compile stylesheet. 1 error detected. ... Description: No character data is allowed between top-level elements Start location: 4:0
    




    XML file: arbitrary (not referenced anywhere)


    XQuery module: "hello.xq" (tested and working)

    
    xquery version  
    "1.0"; module namespace hw=
    "http://www.xyz.com/hello";   declare function hw:hello () as xs:string* 
    { 
    "Hello World" 
    };
    



    XSLT stylesheet: "import.xsl"

    
    <?xml version=
    "1.0" encoding=
    "UTF-8"?> <xsl:stylesheet xmlns:xsl=
    "http://www.w3.org/1999/XSL/Transform" xmlns:saxon=
    "http://saxon.sf.net/" version=
    "1.0"> saxon:import-query(
    "hello.xq", 
    "hw")   <xsl:template match=
    "/"> <html> <em>hw:hello()</em> </html> </xsl:template> </xsl:stylesheet>
    
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-24T07:44:16Z  
    saxon:mport-query is a declaration with attributes, not a function with parameters. So it's

    
    <saxon:import-query href=
    "query.xq" namespace=
    "http://my-module/"/>
    
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-24T13:45:56Z  
    saxon:mport-query is a declaration with attributes, not a function with parameters. So it's

    <pre class="jive-pre"> <saxon:import-query href= "query.xq" namespace= "http://my-module/"/> </pre>
    Dr. Kay, thanks for the advice. Progress is being made, but "Hello World" remains elusive. One more round might do it, if you have time.

    Here are the latest iterations of the query library module containing a helloWorld() function; an XQuery that calls helloWorld() successfully; and an XSLT stylesheet that attempts to call helloWorld(), but gives the following error when executed:

    
    Engine name: Saxon6.5.5 Severity: error Description: The URI http:
    //hello/ does not identify an external Java class
    


    XQuery library module ("c:\testDir\hello.xq"):

    
    xquery version  
    "1.0"; module namespace hw=
    "http://hello/";   declare function hw:helloWorld() as xs:string* 
    { 
    "Hello World" 
    };
    

    XQuery query ("c:\testDir\import.xq") calling helloWorld():

    
    
    
    import module namespace hw=
    "http://hello/" at 
    "hello.xq";   hw:helloWorld()
    

    XSLT stylesheet ("c:\testDir\import.xsl") calling helloWorld():

    
    <?xml version=
    "1.0" encoding=
    "UTF-8"?> <xsl:stylesheet version=
    "1.0" xmlns:xsl=
    "http://www.w3.org/1999/XSL/Transform" xmlns:saxon=
    "http://saxon.sf.net/" exclude-result-prefixes=
    "saxon" xmlns:hw=
    "http://hello/"> <saxon:import-query href=
    "hello.xq" namespace=
    "http://hello" />   <xsl:template match=
    "/"> <html> <xsl:value-of select=
    'hw:hello()' /> </html> </xsl:template> </xsl:stylesheet>
    
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-24T13:49:32Z  
    Dr. Kay, thanks for the advice. Progress is being made, but "Hello World" remains elusive. One more round might do it, if you have time.

    Here are the latest iterations of the query library module containing a helloWorld() function; an XQuery that calls helloWorld() successfully; and an XSLT stylesheet that attempts to call helloWorld(), but gives the following error when executed:

    <pre class="jive-pre"> Engine name: Saxon6.5.5 Severity: error Description: The URI http: //hello/ does not identify an external Java class </pre>

    XQuery library module ("c:\testDir\hello.xq"):

    <pre class="jive-pre"> xquery version "1.0"; module namespace hw= "http://hello/"; declare function hw:helloWorld() as xs:string* { "Hello World" }; </pre>
    XQuery query ("c:\testDir\import.xq") calling helloWorld():

    <pre class="jive-pre"> import module namespace hw= "http://hello/" at "hello.xq"; hw:helloWorld() </pre>
    XSLT stylesheet ("c:\testDir\import.xsl") calling helloWorld():

    <pre class="jive-pre"> <?xml version= "1.0" encoding= "UTF-8"?> <xsl:stylesheet version= "1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform" xmlns:saxon= "http://saxon.sf.net/" exclude-result-prefixes= "saxon" xmlns:hw= "http://hello/"> <saxon:import-query href= "hello.xq" namespace= "http://hello" /> <xsl:template match= "/"> <html> <xsl:value-of select= 'hw:hello()' /> </html> </xsl:template> </xsl:stylesheet> </pre>
    Sorry, function call in import.xsl should be hw:helloWorld(), not hw:hello(). Same error though.
  • SystemAdmin
    SystemAdmin
    746 Posts

    Re: Small complex lookups sprinkled arbitrarily through long file being XSLT'd

    ‏2012-10-24T14:03:08Z  
    Sorry, function call in import.xsl should be hw:helloWorld(), not hw:hello(). Same error though.
    You're using Saxon 6.5.5 which is XSLT 1.0 only (no XQuery). You will need Saxon-PE for this, the current release is 9.4.0.6, available from www.saxonica.com.

    If you have further enquiries I would be grateful if you could raise them on the Saxon forum at http://saxonica.plan.io/ where we can track them better.