简体   繁体   English

在 Saxon/C 中使用条件包含/静态参数?

[英]Using conditional includes/static parameters in Saxon/C?

I'm trying to use XSLT conditional includes/static parameters with Saxon/C HE, but I'm getting the following error:我正在尝试将 XSLT 条件包含/静态参数与 Saxon/C HE 一起使用,但出现以下错误:

Error 
  Parameter $some_param cannot be supplied dynamically because it is declared as static

To reproduce, I've used an example from a couple of answers I added a few years ago.为了重现,我使用了几年前添加的几个答案中的一个示例。 ( Here and here .) 这里这里。)

In both of those answers I used the java version 9.7 of Saxon-HE from the command line without issue.在这两个答案中,我都从命令行使用了 Saxon-HE 的 java 9.7 版,没有问题。 I also tested again using java version 10.5 of HE from the command line.我还从命令行使用 HE 的 java 版本 10.5 再次测试。 Again no issues.再次没有问题。

However, if I try to run this example from Python (3.8) using Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5CI get the error above.但是,如果我尝试使用与 Saxon-HE 9.9.1.5CI 一起运行的 Saxon/C 1.2.1 从 Python (3.8) 运行此示例,则会出现上述错误。

Does anyone else have experience with static params in XSLT 3.0 and Saxon/C (especially with Python) that can provide guidance?是否有其他人对 XSLT 3.0 和 Saxon/C(尤其是 Python)中的 static 参数有经验,可以提供指导?

Test code...测试代码...

XML Input (test.xml) XML 输入(test.xml)

<doc>
    <foo/>
</doc>

Python Python

import saxonc

saxon_proc = saxonc.PySaxonProcessor(license=False)

print(f"Processor version: {saxon_proc.version}")

xsltproc = saxon_proc.new_xslt30_processor()

# noinspection PyArgumentList
xsltproc.set_parameter("inc2", saxon_proc.make_boolean_value(True))

results = xsltproc.transform_to_string(source_file="test.xml", stylesheet_file="test_main.xsl")

if results:
    print(results)

saxon_proc.release()

Main XSLT 3.0 (test_main.xsl)主 XSLT 3.0 (test_main.xsl)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:param name="inc1" as="xs:boolean" select="false()" 
        static="yes" required="no"/>
    <xsl:param name="inc2" as="xs:boolean" select="false()" 
        static="yes" required="no"/>

    <xsl:include href="test_inc1.xsl" use-when="$inc1"/>
    <xsl:include href="test_inc2.xsl" use-when="$inc2"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

First possible included XSLT 3.0 (test_inc1.xsl)第一个可能包括 XSLT 3.0 (test_inc1.xsl)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="foo">
        <xsl:copy>INCLUDE FILE 1!!!</xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Second possible included XSLT 3.0 (test_inc2.xsl)第二种可能包括 XSLT 3.0 (test_inc2.xsl)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="foo">
        <xsl:copy>INCLUDE FILE 2!!!</xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Expected Output (this is what I get using java Saxon-HE from the command line (shown below))预期 Output (这是我从命令行使用 java Saxon-HE 得到的(如下所示))

<doc>
   <foo>INCLUDE FILE 2!!!</foo>
</doc>

Actual Output实际 Output

Processor version: Saxon/C 1.2.1 running with Saxon-HE 9.9.1.5C from Saxonica
Error 
  Parameter $inc2 cannot be supplied dynamically because it is declared as static

Working command line:工作命令行:

java -cp "saxon-he-10.5.jar" net.sf.saxon.Transform -s:"test.xml" -xsl:"test_main2.xsl" inc2="true"

I should also note that I get the same error when trying to use a shadow attribute (command line still works using the command line arg inc_number="2" ):我还应该注意,我在尝试使用影子属性时遇到了同样的错误(命令行仍然可以使用命令行 arg inc_number="2" ):

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="inc_number" as="xs:string" select="'1'" static="yes" required="no"/>

    <xsl:include _href="test_inc{$inc_number}.xsl"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

From @ond1 (O'Neil Delpratt - Saxonica)...来自@ond1(O'Neil Delpratt - Saxonica)...

Hi, unfortunately it is not currently possible to set the static parameters in the current version, but I have added this feature which will be available in the next release.您好,很遗憾,目前无法在当前版本中设置 static 参数,但我已添加此功能,将在下一个版本中提供。

So until setting static params is supported, I'm going to use two different "main" XSLTs;因此,在支持设置 static 参数之前,我将使用两个不同的“主要”XSLT; each one including the desired stylesheet.每一个都包括所需的样式表。 Another option is to transform the XSLT before executing it, but the two separate "main"'s is what I'm already doing anyway.另一种选择是在执行 XSLT 之前对其进行转换,但是无论如何我已经在做这两个单独的“主要”。

Also, once setting static params is supported I'll update this answer with a full working example.此外,一旦支持设置 static 参数,我将使用完整的工作示例更新此答案。

My first test with SaxonC 11.1 and the above XSLT code and using the Python API results in the Python code My first test with SaxonC 11.1 and the above XSLT code and using the Python API results in the Python code

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)
    
    xslt30proc = proc.new_xslt30_processor()
    
    result = xslt30proc.transform_to_string(stylesheet_file = 'main-sheet.xsl', source_file = 'test.xml')
         
    print(result)
    
    xslt30proc.set_parameter('inc1', proc.make_boolean_value(True))
    
    result = xslt30proc.transform_to_string(stylesheet_file = 'main-sheet.xsl', source_file = 'test.xml')
  
    print(result)
    
    xslt30proc.set_parameter('inc2', proc.make_boolean_value(True))
    
    result = xslt30proc.transform_to_string(stylesheet_file = 'main-sheet.xsl', source_file = 'test.xml')
  
    print(result)

and outputs和输出

Test SaxonC on Python
SaxonC-HE 11.1 from Saxonica
source in transformFiletoString=test.xml stylsheet=main-sheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo/>
</doc>

source in transformFiletoString=test.xml stylsheet=main-sheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo>INCLUDE FILE 1!!!</foo>
</doc>

source in transformFiletoString=test.xml stylsheet=main-sheet.xsl
Warning at mode (unnamed)
  XTDE0540  Ambiguous rule match for /doc/foo[1]
Matches both "foo" on line 3 of
  file:/C:/Users/marti/OneDrive/Documents/xslt/blog-xslt-3-by-example/saxonc-11.1-python/conditional-include-with-static-params/test_inc2.xsl
and "foo" on line 3 of file:/C:/Users/marti/OneDrive/Documents/xslt/blog-xslt-3-by-example/saxonc-11.1-python/conditional-include-with-static-params/test_inc1.xsl
<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo>INCLUDE FILE 2!!!</foo>
</doc>

Or just your example of setting the parameter and running the stylesheet:或者只是设置参数和运行样式表的示例:

from saxonc import *

with PySaxonProcessor(license=False) as proc:
    print("Test SaxonC on Python")
    print(proc.version)
    
    xslt30proc = proc.new_xslt30_processor()
    
    xslt30proc.set_parameter('inc2', proc.make_boolean_value(True))
    
    result = xslt30proc.transform_to_string(stylesheet_file = 'main-sheet.xsl', source_file = 'test.xml')
  
    print(result)

gives

Test SaxonC on Python
SaxonC-HE 11.1 from Saxonica
source in transformFiletoString=test.xml stylsheet=main-sheet.xsl
<?xml version="1.0" encoding="UTF-8"?>
<doc>
   <foo>INCLUDE FILE 2!!!</foo>
</doc>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM