问题一:
问题2:为什么
问题描述:
我有一个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">
<!DOCTYPE html>
</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>
我得到这个结果(注意
<?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/