Xml 使用XSLT合并html文件?
我正在将多个html文件转换为一个文件,即一本书中的多个章节。为此,我收到了文本文件,其中是文件列表的顺序。转换时,我没有得到正确的章节顺序: TXT文件:Xml 使用XSLT合并html文件?,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我正在将多个html文件转换为一个文件,即一本书中的多个章节。为此,我收到了文本文件,其中是文件列表的顺序。转换时,我没有得到正确的章节顺序: TXT文件: FilePath=d:\Amrendra\edgar xml-html\All\Edger_Final\xml\07_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\02_Document_Edgar17Nov.out.indd,d:\A
FilePath=d:\Amrendra\edgar xml-html\All\Edger_Final\xml\07_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\02_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\03_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\04_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\05_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\06_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\01_FrontMatter_Edgar17Nov.out.indd
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:character-map name="m1">
<xsl:output-character character="•" string="&bull;"/>
<xsl:output-character character=" " string="&nbsp;"/>
<xsl:output-character character="’" string="&rsquo;"/>
</xsl:character-map>
<xsl:output method="xhtml" use-character-maps="m1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="filelist">
<xsl:analyze-string select="unparsed-text('../book_bulidIndesign.txt')" regex="FilePath=(.*)">
<xsl:matching-substring>
<xsl:value-of select="normalize-space(regex-group(1))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="file-seq">
<map>
<xsl:for-each select="tokenize($filelist, ',')">
<file>
<xsl:attribute name="pos" select="position()"/>
<xsl:value-of select="iri-to-uri(concat('file:///', replace(replace(replace(., '\\InDesign\\', '\\XML\\'), 'indd$', 'html'), '\\', '/')))"/>
</file>
</xsl:for-each>
</map>
</xsl:variable>
<xsl:template match="/">
<html>
<body style="font: 10pt Times New Roman, Times, Serif">
<xsl:for-each select="document($file-seq/map/file)">
<xsl:apply-templates select="/node()/body/node()"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
用于合并的XSLT:
FilePath=d:\Amrendra\edgar xml-html\All\Edger_Final\xml\07_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\02_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\03_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\04_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\05_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\06_Document_Edgar17Nov.out.indd,d:\Amrendra\edgar xml-html\All\Edger_Final\xml\01_FrontMatter_Edgar17Nov.out.indd
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:character-map name="m1">
<xsl:output-character character="•" string="&bull;"/>
<xsl:output-character character=" " string="&nbsp;"/>
<xsl:output-character character="’" string="&rsquo;"/>
</xsl:character-map>
<xsl:output method="xhtml" use-character-maps="m1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="filelist">
<xsl:analyze-string select="unparsed-text('../book_bulidIndesign.txt')" regex="FilePath=(.*)">
<xsl:matching-substring>
<xsl:value-of select="normalize-space(regex-group(1))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="file-seq">
<map>
<xsl:for-each select="tokenize($filelist, ',')">
<file>
<xsl:attribute name="pos" select="position()"/>
<xsl:value-of select="iri-to-uri(concat('file:///', replace(replace(replace(., '\\InDesign\\', '\\XML\\'), 'indd$', 'html'), '\\', '/')))"/>
</file>
</xsl:for-each>
</map>
</xsl:variable>
<xsl:template match="/">
<html>
<body style="font: 10pt Times New Roman, Times, Serif">
<xsl:for-each select="document($file-seq/map/file)">
<xsl:apply-templates select="/node()/body/node()"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
注意:如果我尝试打印章节的映射,它会工作得很好!所有html文件在特定路径上都可用。在此结构中:
<xsl:for-each select="document($file-seq/map/file)">
<xsl:apply-templates select="/node()/body/node()"/>
</xsl:for-each>
该规范规定:“由[document]函数返回的节点序列是按文档顺序的,没有重复项。此顺序与在…参数中提供URI的顺序没有必要的关系。”
如果您想要一个特定的订单(保留副本),可以使用
<xsl:for-each select="$file-seq/map/file ! document(.)">
<xsl:apply-templates select="/node()/body/node()"/>
</xsl:for-each>
“!”运算符是XPath 3.0,在2.0中,您可以执行以下操作:
<xsl:for-each select="$file-seq/map/file">
<xsl:apply-templates select="document(.)/node()/body/node()"/>
</xsl:for-each>
事实上,我不是100%相信这能解决你的问题。可能还有其他影响结果顺序的因素,我忽略了。但这肯定是您对结果顺序做出不必要假设的一个方面。要根据文件中的文本内容对输出进行排序,只需在模板中的每个的
后面添加以下行:
<xsl:sort select="/node()/body/node()/text()" />
感谢大家的建议,我按照顺序将文档存储在变量中,并在应用这些方法链接后:
<xsl:variable name="filelist">
<xsl:analyze-string select="unparsed-text('../book_bulidIndesign.txt')" regex="FilePath=(.*)">
<xsl:matching-substring>
<xsl:value-of select="normalize-space(regex-group(1))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:variable name="all-chapter">
<xsl:for-each select="tokenize($filelist, ',')">
<xsl:variable name="c_path">
<xsl:value-of select="concat('file:///', iri-to-uri(replace(replace(replace(., '\\InDesign\\', '\\XML\\'), 'indd$', 'html'), '\\', '/')))"/>
</xsl:variable>
<xsl:copy-of select="document($c_path)"/>
</xsl:for-each>
</xsl:variable>
<xsl:template match="/">
<html>
<body style="font: 10pt Times New Roman, Times, Serif">
<xsl:apply-templates select="$all-chapter/node()/body/node()"/>
<!--<xsl:apply-templates select="$all-chapter/node()"/>-->
</body>
</html>
</xsl:template>
我用一些测试文件测试了您的代码,它运行得很好。无法复制,那么您会遇到什么错误?合并html文件时,请查看输出内容是否按所述输入txt文件的顺序排序???我也产生了输出,但序列是错误的,按照txt顺序。目前我正在使用Oxygen 18 Editor对其进行转换。输出是按照文本文件字符串中提到的文件的顺序进行的。感谢您的建议,但是这种比较在XSLT 2.0 document()中不适用于变量路径。很抱歉,是的,“!”运算符是XPath 3.0。在2.0中,您必须在$file seq/map/file return document($f)中为$f编写,
或者,请参见编辑后的答案。