XSLT、循环中的多个 XML 文件、命名空间问题

标签 xslt xml-namespaces

问题一:

中出现xmns=""是什么原因?

问题2:为什么

中的“epub”命名空间消失了?

问题描述:

我有一个index.xml,其中包含我想要使用一个XSLT 文件处理的XML 文件列表。 整个转换和循环等都很顺利。这是index.xml 文件的开头:

      <?xml version="1.0"?>
      <?xml-stylesheet type="text/xsl" href="file:///E:/toyota.xslt"?>
      <list>
        <entry>
            <file>01.xml</file>
        </entry>
        <entry>
            <file>02.xml</file>
        </entry>
        ... etc.

文件 01.xml 是 html 标签和“特殊”标签(我自己的)的混合体。

文件 01.xml 如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <body>
        <section epub:type="frontmatter">
            <p>....</p>
            <p>....</p>
            <p>....</p>
        </section>
    </body>

这是 toyota.xslt 的一部分(已经变得相当长)。在这里您可以看到循环机制和处理 的 部分的模板:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:transform version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:epub="http://www.idpf.org/2007/ops" exclude-result-prefixes="xs fn">
        <xsl:output method="xhtml" version="1.0" encoding="UTF-8" indent="yes"/>

        <xsl:template match="/">
            <xsl:apply-templates select="/list/entry"/>
        </xsl:template>
        <xsl:template match="entry">
            <xsl:variable name="filename" select="concat('new/' , substring-before( file, '.'), '.xhtml')"/>
            <xsl:result-document href="{$filename}" >
            <xsl:text disable-output-escaping="yes">
                &lt;!DOCTYPE html&gt;
            </xsl:text>
                <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="nl" xml:lang="nl">
                    <head>
                        <meta charset="utf-8" />
                        <title>
                            <xsl:value-of select="./title"/>
                        </title>

                    </head>
                    <xsl:apply-templates select="document(file)/body"/>
                </html>
            </xsl:result-document>
        </xsl:template>
        <xsl:template match="/body">
            <body>
                <xsl:apply-templates select="/body/@* | /body/node()"/>
            </body>
        </xsl:template>

我得到这个结果(注意

上多余的 xmlns 属性。我也在其他标签上得到它们,但我猜它们有相同的原因):

     <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE html>
            <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" lang="nl" xml:lang="nl">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            <section xmlns="" type="frontmatter">
                <p> .... </p>
                <p> .... </p>       
            </section>
        </body>
     </html>              

我对 XSLT 还很陌生,但我很自豪这个系统到目前为止能正常运行。在帮助下。现在,这个命名空间问题...... 顺便说一句:我使用 XMLSpy 2011。

最佳答案

您是否将节点从辅助输入文件复制到输出?辅助输入文件在任何命名空间中都没有元素,而结果树的根位于 XHTML 命名空间中,因此,要在任何命名空间中保留任何副本的命名空间,序列化程序必须添加 xmlns="" 。解决方案不是复制元素,而是需要使用例如来转换它们

<xsl:template match="section | p">
  <xsl:element name="{local-name()}" namespace="http://www.w3.org/1999/xhtml">
    <xsl:apply-templates select="@* | node()"/>
  </xsl:element>
</xsl:template>

关于XSLT、循环中的多个 XML 文件、命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22280525/

相关文章:

xslt 和 xpath : match preceding comments

python - 带有 django 模板变量的 xslt

xml - XSLT - 属性的出现

xml - XPath 如何处理 XML 命名空间?

xml - xmlns 中的 x 是 :x special?

xslt - xsl中的条件语句

xml - 使用 XSLT 排除 XML 空节点

Java QName 前缀不起作用

html - CSS3 namespace 是否用于除 XML 之外的任何内容?

c# - 将命名空间设置为 XElement