2

需求描述:我们的需求是在输入的xml文档中寻找目标为PubTbl且具有伪属性@rth的处理指令。这些处理指令特定于 Arbortext Editor,它们以表格形式呈现在整个 XML 文档中。

<?PubTbl row rht="0.76in"?>

我们注意到,在我们的 xml 文档中的处理指令中,我们的 ISO Schematron 模式(如下所述)没有被触发。

  • ISO Schematron 不验证处理指令吗?
  • 如果 ISO Schematron 验证了处理指令,我需要注意任何设置/参数更改吗?

ISO Schematron 模式

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" id="vert-space-tables_schema"
    queryBinding="xslt2" schemaVersion="iso">
    <title>Forcing vertical space in content within tables</title>
    <pattern id="vert-space-tables_pattern">
        <title>Forcing vertical space in content within tables</title>
        <rule id="vert-space-tables_processing-inst_rule" context="processing-instruction('PubTbl')"
            abstract="false">
            <report test="contains(.,'rht=')"> 
                The table have got processing instructions you are looking altered to modify
                pagination. </report>
        </rule>
    </pattern>
</schema>

我正在使用附加的“ant-schematron-2010-04-14.jar”zip 文件中的 ISO schematron 样式表,使用 ant 任务

   <taskdef name="schematron" classname="com.schematron.ant.SchematronTask"
        classpath="${lib}/ant-schematron-2010-04-14.jar; ${lib}/resolver.jar"/>

<schematron schema="${schema}/vert-space-tables/vert-space-tables.sch" failonerror="false"
            queryLanguageBinding="xslt2" format="svrl" OutputDir="${dist}/vert-space-tables"
            outputFilename="fail.xml" debugMode="true" failOnError="false" diagnose="true" classpathref="saxon-loc">
            <fileset dir="${xml}/vert-space-tables" includes="Forcing_vertical_space_in_content_within_tables-fail.xml"/>            
            </schematron>

“debug.xslt”——还附加了从 ISO schematron 生成并针对 xml 文档进行验证的 XSL 样式表。

在 debug.xslt 中(在文件的底部),我注意到 apply-templates 只处理元素节点,它永远不会处理处理指令,这是为什么呢?

<xsl:template match="@*|node()" priority="-2" mode="M1">
              <xsl:apply-templates select="*****" mode="M1"/>
           </xsl:template>

debug.xslt 内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:saxon="http://saxon.sf.net/"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:schold="http://www.ascc.net/xml/schematron"
                xmlns:iso="http://purl.oclc.org/dsdl/schematron"
                xmlns:xhtml="http://www.w3.org/1999/xhtml"
                version="2.0"><!--Implementers: please note that overriding process-prolog or process-root is 
    the preferred method for meta-stylesheets to use where possible. -->
<xsl:param name="archiveDirParameter"/>
   <xsl:param name="archiveNameParameter"/>
   <xsl:param name="fileNameParameter"/>
   <xsl:param name="fileDirParameter"/>
   <xsl:variable name="document-uri">
      <xsl:value-of select="document-uri(/)"/>
   </xsl:variable>

   <!--PHASES-->


<!--PROLOG-->
<xsl:output xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
               method="xml"
               omit-xml-declaration="no"
               standalone="yes"
               indent="yes"/>

   <!--XSD TYPES FOR XSLT2-->


<!--KEYS AND FUNCTIONS-->


<!--DEFAULT RULES-->


<!--MODE: SCHEMATRON-SELECT-FULL-PATH-->
<!--This mode can be used to generate an ugly though full XPath for locators-->
<xsl:template match="*" mode="schematron-select-full-path">
      <xsl:apply-templates select="." mode="schematron-get-full-path"/>
   </xsl:template>

   <!--MODE: SCHEMATRON-FULL-PATH-->
<!--This mode can be used to generate an ugly though full XPath for locators-->
<xsl:template match="*" mode="schematron-get-full-path">
      <xsl:apply-templates select="parent::*" mode="schematron-get-full-path"/>
      <xsl:text>/</xsl:text>
      <xsl:choose>
         <xsl:when test="namespace-uri()=''">
            <xsl:value-of select="name()"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:text>*:</xsl:text>
            <xsl:value-of select="local-name()"/>
            <xsl:text>[namespace-uri()='</xsl:text>
            <xsl:value-of select="namespace-uri()"/>
            <xsl:text>']</xsl:text>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:variable name="preceding"
                    select="count(preceding-sibling::*[local-name()=local-name(current())                                   and namespace-uri() = namespace-uri(current())])"/>
      <xsl:text>[</xsl:text>
      <xsl:value-of select="1+ $preceding"/>
      <xsl:text>]</xsl:text>
   </xsl:template>
   <xsl:template match="@*" mode="schematron-get-full-path">
      <xsl:apply-templates select="parent::*" mode="schematron-get-full-path"/>
      <xsl:text>/</xsl:text>
      <xsl:choose>
         <xsl:when test="namespace-uri()=''">@<xsl:value-of select="name()"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:text>@*[local-name()='</xsl:text>
            <xsl:value-of select="local-name()"/>
            <xsl:text>' and namespace-uri()='</xsl:text>
            <xsl:value-of select="namespace-uri()"/>
            <xsl:text>']</xsl:text>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <!--MODE: SCHEMATRON-FULL-PATH-2-->
<!--This mode can be used to generate prefixed XPath for humans-->
<xsl:template match="node() | @*" mode="schematron-get-full-path-2">
      <xsl:for-each select="ancestor-or-self::*">
         <xsl:text>/</xsl:text>
         <xsl:value-of select="name(.)"/>
         <xsl:if test="preceding-sibling::*[name(.)=name(current())]">
            <xsl:text>[</xsl:text>
            <xsl:value-of select="count(preceding-sibling::*[name(.)=name(current())])+1"/>
            <xsl:text>]</xsl:text>
         </xsl:if>
      </xsl:for-each>
      <xsl:if test="not(self::*)">
         <xsl:text/>/@<xsl:value-of select="name(.)"/>
      </xsl:if>
   </xsl:template>
   <!--MODE: SCHEMATRON-FULL-PATH-3-->
<!--This mode can be used to generate prefixed XPath for humans 
    (Top-level element has index)-->
<xsl:template match="node() | @*" mode="schematron-get-full-path-3">
      <xsl:for-each select="ancestor-or-self::*">
         <xsl:text>/</xsl:text>
         <xsl:value-of select="name(.)"/>
         <xsl:if test="parent::*">
            <xsl:text>[</xsl:text>
            <xsl:value-of select="count(preceding-sibling::*[name(.)=name(current())])+1"/>
            <xsl:text>]</xsl:text>
         </xsl:if>
      </xsl:for-each>
      <xsl:if test="not(self::*)">
         <xsl:text/>/@<xsl:value-of select="name(.)"/>
      </xsl:if>
   </xsl:template>

   <!--MODE: GENERATE-ID-FROM-PATH -->
<xsl:template match="/" mode="generate-id-from-path"/>
   <xsl:template match="text()" mode="generate-id-from-path">
      <xsl:apply-templates select="parent::*" mode="generate-id-from-path"/>
      <xsl:value-of select="concat('.text-', 1+count(preceding-sibling::text()), '-')"/>
   </xsl:template>
   <xsl:template match="comment()" mode="generate-id-from-path">
      <xsl:apply-templates select="parent::*" mode="generate-id-from-path"/>
      <xsl:value-of select="concat('.comment-', 1+count(preceding-sibling::comment()), '-')"/>
   </xsl:template>
   <xsl:template match="processing-instruction()" mode="generate-id-from-path">
      <xsl:apply-templates select="parent::*" mode="generate-id-from-path"/>
      <xsl:value-of select="concat('.processing-instruction-', 1+count(preceding-sibling::processing-instruction()), '-')"/>
   </xsl:template>
   <xsl:template match="@*" mode="generate-id-from-path">
      <xsl:apply-templates select="parent::*" mode="generate-id-from-path"/>
      <xsl:value-of select="concat('.@', name())"/>
   </xsl:template>
   <xsl:template match="*" mode="generate-id-from-path" priority="-0.5">
      <xsl:apply-templates select="parent::*" mode="generate-id-from-path"/>
      <xsl:text>.</xsl:text>
      <xsl:value-of select="concat('.',name(),'-',1+count(preceding-sibling::*[name()=name(current())]),'-')"/>
   </xsl:template>

   <!--MODE: GENERATE-ID-2 -->
<xsl:template match="/" mode="generate-id-2">U</xsl:template>
   <xsl:template match="*" mode="generate-id-2" priority="2">
      <xsl:text>U</xsl:text>
      <xsl:number level="multiple" count="*"/>
   </xsl:template>
   <xsl:template match="node()" mode="generate-id-2">
      <xsl:text>U.</xsl:text>
      <xsl:number level="multiple" count="*"/>
      <xsl:text>n</xsl:text>
      <xsl:number count="node()"/>
   </xsl:template>
   <xsl:template match="@*" mode="generate-id-2">
      <xsl:text>U.</xsl:text>
      <xsl:number level="multiple" count="*"/>
      <xsl:text>_</xsl:text>
      <xsl:value-of select="string-length(local-name(.))"/>
      <xsl:text>_</xsl:text>
      <xsl:value-of select="translate(name(),':','.')"/>
   </xsl:template>
   <!--Strip characters--><xsl:template match="text()" priority="-1"/>

   <!--SCHEMA SETUP-->
<xsl:template match="/">
      <svrl:schematron-output xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                              title="Forcing vertical space in content within tables"
                              schemaVersion="iso">
         <xsl:comment>
            <xsl:value-of select="$archiveDirParameter"/>   
         <xsl:value-of select="$archiveNameParameter"/>  
         <xsl:value-of select="$fileNameParameter"/>  
         <xsl:value-of select="$fileDirParameter"/>
         </xsl:comment>
         <svrl:active-pattern>
            <xsl:attribute name="document">
               <xsl:value-of select="document-uri(/)"/>
            </xsl:attribute>
            <xsl:attribute name="id">vert-space-tables_pattern</xsl:attribute>
            <xsl:attribute name="name">Forcing vertical space in content within tables</xsl:attribute>
            <xsl:apply-templates/>
         </svrl:active-pattern>
         <xsl:apply-templates select="/" mode="M1"/>
      </svrl:schematron-output>
   </xsl:template>

   <!--SCHEMATRON PATTERNS-->
<svrl:text xmlns:svrl="http://purl.oclc.org/dsdl/svrl">Forcing vertical space in content within tables</svrl:text>

   <!--PATTERN vert-space-tables_patternForcing vertical space in content within tables-->
<svrl:text xmlns:svrl="http://purl.oclc.org/dsdl/svrl">Forcing vertical space in content within tables</svrl:text>

      <!--RULE vert-space-tables_processing-inst_rule-->
<xsl:template match="processing-instruction('PubTbl')" priority="1000" mode="M1">
      <svrl:fired-rule xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                       context="processing-instruction('PubTbl')"
                       id="vert-space-tables_processing-inst_rule"/>

            <!--REPORT -->
<xsl:if test="contains(., 'breakpenalty=&#34;-10000&#34;') or contains(., 'breakpenalty=&#34;2000&#34;') or contains(.,'rht=')">
         <svrl:successful-report xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                                 test="contains(., 'breakpenalty=&#34;-10000&#34;') or contains(., 'breakpenalty=&#34;2000&#34;') or contains(.,'rht=')">
            <xsl:attribute name="location">
               <xsl:apply-templates select="." mode="schematron-select-full-path"/>
            </xsl:attribute>
            <svrl:text> 
                The table have got processing instructions you are looking altered to modify
                pagination. </svrl:text>
         </svrl:successful-report>
      </xsl:if>
      <xsl:apply-templates select="*" mode="M1"/>
   </xsl:template>
   <xsl:template match="text()" priority="-1" mode="M1"/>
   <xsl:template match="@*|node()" priority="-2" mode="M1">
      <xsl:apply-templates select="*" mode="M1"/>
   </xsl:template>
</xsl:stylesheet>

谢谢和问候,苏雷什。

4

1 回答 1

1

当我运行这个 schematron 文件(从上面复制)时,我无法复制这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron" id="vert-space-tables_schema"
  queryBinding="xslt2" schemaVersion="iso">
  <title>Forcing vertical space in content within tables</title>
  <pattern id="vert-space-tables_pattern">
    <title>Forcing vertical space in content within tables</title>
    <rule id="vert-space-tables_processing-inst_rule" context="processing-instruction('PubTbl')"
      abstract="false">
      <report test="contains(.,'rht=')"> 
        The table have got processing instructions you are looking altered to modify
        pagination. </report>
    </rule>
  </pattern>
</schema>

针对这个xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <?PubTbl row rht="0.76in"?>  
</root>

使用 Oxygen 14 的 schematron 实现,我收到以下消息:

Warning: unrecognized element svrl:schematron-output

当我下载http://schematron.googlecode.com/files/ant-schematron-2010-04-14.jar并从中使用 iso_svrl_for_xslt2.xsl 来编译 schematron,然后针对示例 xml 运行它,我得到以下信息服务:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<svrl:schematron-output xmlns:svrl="http://purl.oclc.org/dsdl/svrl"
                        xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                        xmlns:saxon="http://saxon.sf.net/"
                        xmlns:schold="http://www.ascc.net/xml/schematron"
                        xmlns:iso="http://purl.oclc.org/dsdl/schematron"
                        xmlns:xhtml="http://www.w3.org/1999/xhtml"
                        title="Forcing vertical space in content within tables"
                        schemaVersion="iso"><!--   
           
           
         --><svrl:active-pattern document="file:/Users/nunes/Desktop/tmp/test.xml"
                        id="vert-space-tables_pattern"
                        name="Forcing vertical space in content within tables"/>
</svrl:schematron-output>

然后,当我从同一个 jar 中针对 iso_schematron_message.xsl 运行时,我也收到以下消息:

Warning: unrecognized element svrl:schematron-output

当我将您的 debug.xslt 与使用指定 jar 中的样式表生成的 xsl 进行比较时(在对它们都运行 Oxygen 的 tidy 以标准化空白之后),我看到了这些差异:

--- debug.xslt  2012-11-10 00:19:39.000000000 -0800
+++ iso.xsl     2012-11-10 00:19:14.000000000 -0800
@@ -310 +310 @@
-      test="contains(., 'breakpenalty=&#34;-10000&#34;') or contains(., 'breakpenalty=&#34;2000&#34;') or contains(.,'rht=')">
+      test="contains(.,'rht=')">
@@ -312 +312 @@
-        test="contains(., 'breakpenalty=&#34;-10000&#34;') or contains(., 'breakpenalty=&#34;2000&#34;') or contains(.,'rht=')"
+        test="contains(.,'rht=')"

您确定您使用的是上面粘贴的 schematron 并使用正确的样式表解析它吗?

于 2012-11-10T08:24:04.253 回答