在结果文档中生成空/空名称空间声明 [英] Empty/blank namespace declarations being generated within result-document

查看:64
本文介绍了在结果文档中生成空/空名称空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个用于将XML转换为ePub的程序包.除了某些情况下,空白的名称空间(xmlns="")节点被写入结果文档之外,其他所有内容都可以正常工作.在进行转换之前,我准备了用于保存主段的临时变量(即metabody等),最后将这些节点(使用xsl:copy-of[@copy-namespaces='no']指令)复制到了结果文档中.我还在xsl:transform元素中使用了@exclude-result-prefixes='ns_list_sep_by_space',但仍然无法获得预期的结果.

I have written a package for converting XMLs to ePubs. Everything works fine, except some cases, where blank namespace (xmlns="") nodes are being written to the result-documents. Prior to transformation, I prepared temporary variables for holding main-segments(i.e., meta, body etc.) and finally copied the nodes(using xsl:copy-of[@copy-namespaces='no'] instruction) to result-document. I also have used @exclude-result-prefixes='ns_list_sep_by_space' within xsl:transform element and still not able to get desired result.

oXygen IDE在弹出窗口中显示一条消息:

oXygen IDE shows a message in pop-up saying:

当使用xsl:copy-of时,新元素还将具有从原始元素节点复制的名称空间节点,除非通过指定copy-namespaces ="no"来排除它们.如果忽略此属性或采用值yes,则将原始元素的所有名称空间节点复制到新元素.如果取值为no,则不会复制任何名称空间节点:但是,仍将按照名称空间修复过程的要求在结果树中创建名称空间节点.

When using xsl:copy-of the new elements will also have namespace nodes copied from the original element node, unless they are excluded by specifying copy-namespaces="no". If this attribute is omitted, or takes the value yes, then all the namespace nodes of the original element are copied to the new element. If it takes the value no, then none of the namespace nodes are copied: however, namespace nodes will still be created in the result tree as required by the namespace fixup process.


以下是我的问题的更多详细信息:


Here is some more details of my problem:

主要样式表:
main.xsl:main caller

Main stylesheet:
main.xsl:main caller

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:cylian="local-ns-for-extension-functions"
    exclude-result-prefixes="xs xd cylian"
    version="2.0">

    <xsl:import href="modules/core.xsl"/>

    <xsl:variable name="base" select="base-uri()" as="xs:anyURI"/>

    <xsl:template match="/">
        <xsl:call-template name="procA"/>
    </xsl:template>

</xsl:transform>

主要样式表:
core.xsl: core processing unit

Main stylesheet:
core.xsl: core processing unit

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
        xmlns:cylian="local-ns-for-extension-functions"
        exclude-result-prefixes="xs xd cylian"
        version="2.0">

      <xsl:import href="sub1.xsl"/>  
      <xsl:import href="sub2.xsl"/>  
      <!--and more-->  

      <!-- variable to hold intermediate results for stage1 -->
      <xsl:variable name="stage1">
          <cylianz>
              <xsl:copy-of select="$a" copy-namespaces="no"/>
              <xsl:copy-of select="$b" copy-namespaces="no"/>
              <!--and more-->
          </cylianz>
      </xsl:variable>

      <!-- variable to hold intermediate results for stage2 -->
      <xsl:variable name="stage2">
          <cylianz>
            <xsl:for-each select="$stage1//cylian">
                <xsl:sort select="@pos"/>
                <xsl:sequence select="."/>
            </xsl:for-each>
          </cylianz>
      </xsl:variable>
      <xsl:template name="procA">
          <xsl:for-each select="$stage2//cylian">
              <xsl:result-document href="{concat($outdir,@href)}" format="general">
                  <xsl:call-template name="procB">
                        <xsl:with-param name="context" select="."/>
                        <xsl:with-param name="title">
                            <xsl:value-of select="$book_title"/>
                        </xsl:with-param>
                   </xsl:call-template>
              </xsl:result-document>
          </xsl:for-each>
      </xsl:template>
     <xsl:template name="procB">
         <xsl:param name="context"/>
         <xsl:param name="title"/>
         <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
              <xsl:call-template name="header">
                  <xsl:with-param name="title" select="$title"/>
               </xsl:call-template>
         </head>
         <body>
              <div id="root">
                  <xsl:apply-templates select="."/>
              </div>
         </body>
    </html>
</xsl:template>

<!--
 1/ other rules are shortened for clarity
 2/ declaration «xmlns:cylian='local-ns-for-extension-functions'» has to retain, some parts of transformation uses some extension functions from that namespace
-->

</xsl:transform>

,这是输出: a.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta xmlns="" http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title xmlns="">BookTitle</title>
          <!--
              2012.04.16 - 18:27:36 [XSLT processor: SAXON 9.1.0.5 from Saxonica]
          -->
      <link xmlns="" href="isbn.css" type="text/css" rel="stylesheet"/>
   </head>
   <body>
      <div id="root">
         <div xmlns="" id="a1">
            <!--...-->
         </div>
      </div>
   </body>
</html>

我希望可以更容易地了解问题所在.欢迎提出所有建议.预先感谢.

I hope it would be easier to understand what's the problem is going on. All suggestions are welcome. Thanks in advance.

推荐答案

好吧,我们需要确认您的代码,但我怀疑您有例如

Well we need to see your code to be sure but I suspect you have e.g.

<xsl:template match="/">
  <foo xmlns="http://example.com/ns">
    <xsl:apply-templates/>
  </foo>
</xsl:template>

<xsl:template match="whatever">
  <bar/>
</xsl:template>

然后您得到

<foo xmlns="http://example.com/ns">
  <bar xmlns=""/>
</foo>

随时随地

<foo xmlns="http://example.com/ns">
  <bar/>
</foo>

要解决此问题,请确保将xsl:stylesheet元素上的默认名称空间声明移动为例如

To fix that make sure you move the default namespace declaration on the xsl:stylesheet element with e.g.

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://example.com/ns">
  version="1.0">

    <xsl:template match="/">
      <foo>
        <xsl:apply-templates/>
      </foo>
    </xsl:template>

    <xsl:template match="whatever">
      <bar/>
    </xsl:template>

</xsl:stylesheet>

以这种方式将其应用于在不同模板中创建的所有结果元素.

that way it applies to all result elements created in different templates.

根据您现在提供的示例,我认为我的建议是正确的,只有几个文件才需要确保将xmlns="http://www.w3.org/1999/xhtml"分别放在xsl:stylesheet元素上的所有样式表模块xsl:transform元素,以便所有结果元素都结束在XHTML命名空间中.

[edit] Based on the samples you have provided now I think my suggestion is right, only with several files you need to make sure that all stylesheet modules you have put xmlns="http://www.w3.org/1999/xhtml" on the xsl:stylesheet respectively xsl:transform elements so that all result elements end up in the XHTML namespace.

[第二次编辑] 我想你想要

[second edit] I think you want

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
        xmlns:cylian="local-ns-for-extension-functions"
        exclude-result-prefixes="xs xd cylian"
        version="2.0">

      <xsl:import href="sub1.xsl"/>  
      <xsl:import href="sub2.xsl"/>  
      <!--and more-->  

      <!-- variable to hold intermediate results for stage1 -->
      <xsl:variable name="stage1" xmlns="">
          <cylianz>
              <xsl:copy-of select="$a" copy-namespaces="no"/>
              <xsl:copy-of select="$b" copy-namespaces="no"/>
              <!--and more-->
          </cylianz>
      </xsl:variable>

      <!-- variable to hold intermediate results for stage2 -->
      <xsl:variable name="stage2" xmlns="">
          <cylianz>
            <xsl:for-each select="$stage1//cylian">
                <xsl:sort select="@pos"/>
                <xsl:sequence select="."/>
            </xsl:for-each>
          </cylianz>
      </xsl:variable>
      <xsl:template name="procA">
          <xsl:for-each select="$stage2//cylian">
              <xsl:result-document href="{concat($outdir,@href)}" format="general">
                  <xsl:call-template name="procB">
                        <xsl:with-param name="context" select="."/>
                        <xsl:with-param name="title">
                            <xsl:value-of select="$book_title"/>
                        </xsl:with-param>
                   </xsl:call-template>
              </xsl:result-document>
          </xsl:for-each>
      </xsl:template>
     <xsl:template name="procB">
         <xsl:param name="context"/>
         <xsl:param name="title"/>
         <html >
         <head>
              <xsl:call-template name="header">
                  <xsl:with-param name="title" select="$title"/>
               </xsl:call-template>
         </head>
         <body>
              <div id="root">
                  <xsl:apply-templates select="."/>
              </div>
         </body>
    </html>
</xsl:template>


</xsl:transform>

然后,如果您有任何其他模块应产生XHTML元素,请确保将xmlns="http://www.w3.org/1999/xhtml"放在模块的根元素上,或者是否需要在其他命名空间中创建元素,然后在任何应输出XHTML的模板上

And then if you have any additional modules supposed to produce XHTML elements make sure you put xmlns="http://www.w3.org/1999/xhtml" on the root element of the module or if you need to create elements in other namespaces as well then on any template supposed to output XHTML.

这篇关于在结果文档中生成空/空名称空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆