【问题标题】:adding xpath-default-namespace attribute to the final compiled/transformed schematron xslt file将 xpath-default-namespace 属性添加到最终编译/转换的 schematron xslt 文件
【发布时间】:2015-12-08 09:14:46
【问题描述】:

我必须使用 appinfo 元素下的可用信息为各种 xml 模式文件生成 schematron 文件(我进行 xsl 转换以生成 schematron 文件,稍后再次编译)。

schematron 断言所需的 xpath 规则写在这个 appinfo 元素下。但是,这些 xpath 规则不包含任何名称空间前缀。因此,我不能使用 schematron 'ns' 标记将命名空间添加到已编译的最终 xslt 文件中。

解决方案是将 xpath-default-namespace 属性添加到最终编译的 xslt。不幸的是,我找不到任何用于添加 xpath-default-namespace 属性的标签。

这种情况有什么解决方法吗?谢谢。

【问题讨论】:

    标签: xslt xpath schematron


    【解决方案1】:

    目前似乎没有可用于设置xpath-default-namespace 的选项。除了转换生成的 XSLT 之外,另一种选择是修改/扩展 schematron XSLT 以生成所需的输出,以便您可以一次性生成它。

    1. 创建导入iso_schematron_skeleton_for_saxon.xsl 的样式表

    覆盖生成element to insert thexpath-default-namespace`属性的模板:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet  
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias" 
        xmlns:iso="http://purl.oclc.org/dsdl/schematron" 
        xmlns:exsl="http://exslt.org/common" 
        extension-element-prefixes="exsl"
        version="2.0"
        >
        <xsl:import href="iso_schematron_skeleton_for_saxon.xsl"/>
        <!-- Using XSLT 2 -->
        <xsl:template 
            match="iso:schema[@queryBinding='xslt2' or @queryBinding ='xpath2']" 
            priority="10">
            <axsl:stylesheet
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                xmlns:saxon="http://saxon.sf.net/">
                <!-- insert the default namespace attribute -->
                <xsl:attribute name="xpath-default-namespace" select="'http://your/default/namespace/goes/here'"/>
                <xsl:apply-templates 
                    select="iso:ns" />
                <!-- Handle the namespaces before the version attribute: reported to help SAXON -->
                <xsl:attribute name="version">2.0</xsl:attribute>
    
                <xsl:apply-templates select="." mode="stylesheetbody"/>
                <!-- was xsl:call-template name="stylesheetbody"/ -->
            </axsl:stylesheet>
        </xsl:template>
    </xsl:stylesheet>
    
    1. 修改iso_svrl_for_xslt2.xsl 以导入您的覆盖样式表:

    更改导入覆盖 XSLT 的路径:

    <!-- Select the import statement and adjust the path as 
       necessary for your system.
    -->
    <xsl:import href="iso_schematron_skeleton_for_saxon_with_default_namespace.xsl"/>
    

    【讨论】:

      【解决方案2】:

      由于 XSLT 是一个 XML 文件,您可以转换已编译/转换的 schematron XSLT 并自己插入 @xpath-default-namespace

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          version="2.0">
      
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="/*">
              <xsl:copy>
                  <xsl:attribute name="xpath-default-namespace" select="'http://your/default/namespace'"/>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

      • 这肯定有帮助。由于 schematron 断言实际上是 xpath 表达式,我认为必须有一种 schematron 方式来执行此操作(如 ns 的情况)。解决方法只是一种解决方法:)。谢谢。
      • Schematron 样式表目前不支持它,但增强它们以达到您想要的效果并不难,并且避免在另一遍中转换内容。我添加了另一个答案,并解释了一种方法。
      猜你喜欢
      • 1970-01-01
      • 2021-10-21
      • 2020-12-11
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多