简体   繁体   English

Saxon-js 是否对 xsl:param 执行 XML 语法检查?

[英]Does Saxon-js perform XML syntax checking on a xsl:param?

I run this XSL script thru Saxon-js.我通过 Saxon-js 运行这个 XSL 脚本。 It updates a cost field on the main input XHTML using the XML received in the transform call using the stylesheetParams.它使用在使用 stylesheetParams 的转换调用中收到的 XML 更新主输入 XHTML 上的成本字段。 All good.都好。 The problem is that no syntax checking is done on the param-XML (you can see what it looks like in the commented-out line).问题是没有对 param-XML 进行语法检查(您可以在注释掉的行中看到它的样子)。 It is on the XHTML and the transform will generate an error but not on the param-XML.它在 XHTML 上,转换会生成错误,但不会在 param-XML 上。 It just allows it to enter and then the key-function just doesn't update the XHTML.它只是允许它进入,然后键功能不会更新 XHTML。 Is there a way to do the checking for properly formed XML parameter in the same transform call, or do I have to use 2 transform calls: call transform on the param-XSL to syntax-check, then call this main transform to update the XHTML?有没有办法在同一个转换调用中检查格式正确的 XML 参数,还是我必须使用 2 个转换调用:在 param-XSL 上调用转换进行语法检查,然后调用这个主转换来更新 XHTML ?

<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="#all"
  expand-text="yes">
  <xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" include-content-type="no"/>
  <xsl:param name="cost-data"/>
    <!-- <supplier><product><key>3</key><pcost uptype="1"><key>21341</key><cost>12.99</cost></pcost></product></supplier> -->
    <!-- </xsl:param> -->
  
  <xsl:key name="cost" match="product/pcost[@uptype = 1]/cost" use="'cost' || ancestor::product/key"/>
  
  <xsl:mode on-no-match="shallow-copy"/>
        
  <xsl:template match="td[@name][key('cost', @name, fn:parse-xml($cost-data))]/text()">{key('cost', ../@name, fn:parse-xml($cost-data))}%</xsl:template>

<xsl:template match="/" name="xsl:initial-template">
  <xsl:next-match/>
</xsl:template>
</xsl:stylesheet>

If your stylesheet code is如果您的样式表代码是

<xsl:param name="cost-data">
    <supplier><product><key>3</key><pcost uptype="1"><key>21341</key>     
       <cost>12.99</cost></pcost></product></supplier>
</xsl:param>

then that's well-formed XML and no error should be reported.那么这是格式正确的 XML 并且应该不会报告错误。

However, fn:parse-xml($cost-data) is wrong.但是, fn:parse-xml($cost-data)是错误的。 The value of the parameter is a node tree, not a string, and fn:parse-xml() expects lexical XML in a string.该参数的值是一个节点树,而不是一个字符串,并且 fn:parse-xml() 期望字符串中的词法 XML。 The effect of calling fn:parse-xml() on this node tree will be to first atomize the node, producing the untyped atomic value "32134112.99", and then attempt to parse this string "32134112.99" as lexical XML, which should fail.在这个节点树上调用fn:parse-xml()的效果将是首先原子化节点,产生无类型的原子值“32134112.99”,然后尝试将这个字符串“32134112.99”解析为词法 XML,这应该会失败。

To avoid confusion like this it's good practice to always declare the expected type of your parameters, for example as="xs:string" or as="document-node()" .为了避免这样的混淆,最好总是声明参数的预期类型,例如as="xs:string"as="document-node()"

If you want the default value of $cost-data to be a string containing lexical XML, try如果您希望 $cost-data 的默认值是包含词法 XML 的字符串,请尝试

<xsl:param name="cost-data" as="xs:string"><![CDATA[
    <supplier><product><key>3</key><pcost uptype="1"><key>21341</key>     
       <cost>12.99</cost></pcost></product></supplier>
]]></xsl:param>

all on one line without whitespace (or if you need whitespace for readability, put it before a ">" delimiter).全部在一行上,没有空格(或者如果您需要空格以提高可读性,请将其放在“>”分隔符之前)。

Note: the literal answer to your question is: No. Saxon-JS doesn't perform this checking.注意:您问题的字面答案是:不。Saxon-JS 不执行此检查。 The XML parser performs it, long before Saxon-JS gets to see the data. XML 解析器执行它,早在 Saxon-JS 看到数据之前。

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

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