【发布时间】:2020-01-27 22:59:27
【问题描述】:
我的团队使用 XML 编辑器 MadCap Flare 来编写包含 PDF 和 HTML 输出的技术文档。我们需要对某些页面使用 H2 以正确格式化 PDF,但出于 SEO 的目的,我们需要将这些转换为用于 Web 的 H1。我编写了一个构建事件,可在网络发布时将 H2 转换为 H1。但是,我刚刚意识到 XSL 代码错误地去除了变量和图像之间的空格。我发现了 xsl:preserve-space,但使用它会破坏其余代码,因此 H2 永远不会转换为 H1。我需要找到一种方法来执行转换和保留空间。
这是 source HTM 的 sn-p (在你问之前,不,我不能删除 span 标签;它们是由 Flare 在将变量转换为文本时插入的):
<div role="main" id="mc-main-content">
<h2><span class="GlobalCompany">BeyondTrust</span> <span class="ProductsPA">Privileged Remote Access</span> Web Rep Console Requirements</h2>
<p>To run the <span class="GlobalCompany">BeyondTrust</span> <span class="ProductNamesWebConsole">web rep console</span> on your system...</p>
这是我用作构建事件的批处理文件:
@ECHO Off
set outputDir=%1
@set XSLAltova=C:\Users\%username%\AltovaXML.exe
REM Create filelist
dir %outputDir%*.htm /b /s /A-D > file_list.txt
@echo ^<filelist^>^</filelist^> > pre_filelist.xml
REM XML-ize filelist
%XSLAltova% /xslt2 convert_filelist.xsl /in pre_filelist.xml /out pre_list.xml
REM Replace starting h2 tags with h1 tags
%XSLAltova% /xslt2 h2toh1.xsl /in pre_list.xml /out null.xml
REM Garbage collection
DEL pre_list.xml
DEL pre_filelist.xml
DEL file_list.txt
这里是 convert_filelist.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Set output style. XML with no indentations -->
<xsl:output indent="no" method="xml" omit-xml-declaration="yes"/>
<!-- Reads the file list text file into memory as a global variable. -->
<xsl:variable name="fileList">file_list.txt</xsl:variable>
<!-- Parses the file list text file to create an XML list of files that can be fed to the transformer -->
<xsl:template match="filelist">
<!-- Create a variable that can be parsed -->
<xsl:variable name="filelist_raw"><xsl:value-of select="unparsed-text($fileList,'UTF-8')"/></xsl:variable>
<!-- Create a open and close file tags for each line in the list -->
<xsl:variable name="driveLetter"><xsl:value-of select="substring-before(unparsed-text($fileList,'UTF-8'),':')"/>:<xsl:text disable-output-escaping="yes">\\</xsl:text></xsl:variable>
<xsl:variable name="driveLetterReplacement"><xsl:text disable-output-escaping="yes"><file></xsl:text><xsl:value-of select="$driveLetter"/></xsl:variable>
<!-- Generate an xml tree. The value-of is doing a text-level replacement. Looking for the drive letter and replacing it -->
<!-- with the file open tag and drive letter. Looking for the file extension and replacing with the extension and file close tag.-->
<file_list><xsl:value-of select="replace(replace($filelist_raw,'.htm','.htm</file>'),$driveLetter,$driveLetterReplacement)" disable-output-escaping="yes"/></file_list>
</xsl:template>
</xsl:stylesheet>
这里是 h2toh1.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Set output style. -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:preserve-space elements="node()"/>
<!-- Begin traversing the list of files in the output folder. -->
<xsl:template match="file_list">
<xsl:for-each select="*">
<xsl:variable name="filename" select="."/>
<xsl:variable name="content" select="document($filename)"/>
<!-- Generate a new output file to replace the Flare generated file. Uses the same file name. Transparent to the end user. -->
<xsl:result-document href="{$filename}" method="html">
<xsl:apply-templates select="document($filename)">
<xsl:with-param name="content" select="$content"/>
</xsl:apply-templates>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<!-- Recreate each node as it appears in the generated document -->
<xsl:template match="*">
<xsl:param name="content"/>
<xsl:variable name="name" select="name(.)"/>
<xsl:element name="{$name}">
<xsl:for-each select="@*">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- Select the first header and change it to an h1. -->
<xsl:template match="*[matches(name(), 'h\d')][1]">
<xsl:element name="h1">
<xsl:for-each select="@*|node()">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这是没有 xsl:preserve-space 的输出: (h1 style, missing spaces between variables)
这是使用 xsl:preserve-space 的输出: (h2 style, ugly blue for contrast, with spaces)
这是我想要但不能拥有的输出: (h1 style, with spaces)
就目前而言,我的网站有些损坏,而且我没有现成的方法来修复它而不撤消大量工作。任何帮助将不胜感激。
【问题讨论】:
-
我希望
<xsl:preserve-space elements="node()"/>给出错误,因为elements属性的属性值中应该有元素名称而不是节点测试。如果任务是将h2元素转换为h1元素,为什么匹配模式不使用match="h2",为什么要对任何元素*[matches(name(), 'h\d')][1]进行奇怪的检查?如果您在问题中以 HTML 格式以及代码示例/sn-ps 的形式向我们展示结果,这将有所帮助,而不仅仅是渲染的屏幕截图。 -
我使用
*[matches(name(), 'h\d')][1]的原因有两个。首先,我想说明任何滑入其中的 H3。其次,我只想替换第一个标题。我们的许多页面都有 H1,然后是 H2。在这种情况下,我不想更换 H2。这说明清楚了吗? -
另外,我使用
<xsl:preserve-space elements="node()" />的原因也是双重的。首先,我显然误读了another topic。其次,使用星号并没有改变代码(可能是因为那些冗长的 span 标签),而使用node()做了我需要的。 -
很难说,也许问 Altova 支持人员
xsl:preserve-space elements="node()"在他们的实现中做了什么,我只是希望它会产生一个错误。