避免在 topicref 中创建子目录的方法
我的 xslt 代码中有一个非常简单的模板匹配来创建 ditamap,它的工作原理是只要与原始文档的“部分”匹配,就创建一个 topicref。
初始 xml 代码片段如下所示:
<Section Target="IL27TRM409TueAug251754042009251">
<Heading Target="v5170372">WBG (S-GW/P-GW) chassis </Heading>
部分的 xslt 模板匹配如下所示:
<xsl:template match="Section>
<xsl:variable name="file" select="Heading"/>
<xsl:variable name="fileName" select="translate(normalize-space($file),' ','_')"/>
<topicref format="dita" href="{translate(./$fileName,' ','_')}.xml" navtitle="{./Heading}">
<xsl:apply-templates/>
</topicref>
该代码可以正常工作,但对于像上面“WBG (S-GW/P-GW)chassis”这样的标题,它将采用“/”作为一个子文件夹,因此它将创建一个名为“WBG (S-GW”)的子文件夹,然后创建一个文件“P-GW)_chassis.xml”。
有没有办法让 xslt 忽略标题中的“/”并将其视为字符串,只需使用该字符串即可创建 xml 文件?
I have a very simple template match in my xslt code to create a ditamap, and it works by just creating a topicref whenever matches a "Section" of the original document.
The initial xml code fragment looks like:
<Section Target="IL27TRM409TueAug251754042009251">
<Heading Target="v5170372">WBG (S-GW/P-GW) chassis </Heading>
The xslt template match for section looks like:
<xsl:template match="Section>
<xsl:variable name="file" select="Heading"/>
<xsl:variable name="fileName" select="translate(normalize-space($file),' ','_')"/>
<topicref format="dita" href="{translate(./$fileName,' ','_')}.xml" navtitle="{./Heading}">
<xsl:apply-templates/>
</topicref>
The code works, except that for the Heading like above "WBG (S-GW/P-GW) chassis", it will take the "/" as a sub-folder, so it will create a subforlder called "WBG (S-GW" then create a file "P-GW)_chassis.xml".
Is there a way to make xslt ignore the "/" in the Heading and just treat it as a string to create an xml file by simply using that string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Robert,只是为了扩展 @Alejandro 的正确答案/评论:
创建文件夹的不是 XSLT。 XSLT 已经“忽略”标题中的
/
:它不将其视为任何特殊内容。 (查看 XSLT 的输出,您可以看到这一点。)您在什么操作系统(或更准确地说,文件系统)中创建文件?根据这一点,“/”可能是文件名中的非法字符,因此用其他字符替换它可能是您唯一的选择。尝试手动命名文件
a/b
并查看是否可行。由于您已经使用了normalize-space()
,因此似乎并不绝对需要在文件名中精确保留标题内容。要将
-
替换为/
,请修改translate()
表达式,如下所示:这意味着“将
_
替换为每个空格,并将每个/
替换为-
。”Robert, just to expand on @Alejandro's correct answer/comment:
It's not XSLT that's creating a folder. XSLT is already "ignoring" the
/
in the Heading: it does not treat it as anything special. (Look at XSLT's output and you can see that.)What OS (or more properly, file system) are you creating files in? Depending on that, '/' may be an illegal character in filenames, so substituting another character for it may be your only option. Try naming a file
a/b
manually and see if it's even possible. Since you are usingnormalize-space()
already, it seems that you are not absolutely required to preserve the Heading content precisely in the filename.To substitute
-
for/
, modify yourtranslate()
expression like this:This means "substitute
_
for every space, and substitute-
for every/
."