使用 XSLT 创建组合文档 (@href)
Create Composed Documents with XSLT (@href)
如何为我所有引用的文档创建一个节点树并使用 XSLT 将其存储到一个变量中? (我使用 XSLT 2.0)
这是我的文件结构:
- Root 文档 .XML 包含所有特定语言的文档作为 ditamaps
<map>
<navref mapref="de-DE/A.2+X000263.ditamap"/>
<navref mapref="en-US/A.2+X000263.ditamap"/>
<navref mapref="es-ES/A.2+X000263.ditamap"/>
</map>
- 特定语言手册 (.ditamap) - 可能有多个文档
<bookmap id="X000263" xml:lang="de-DE">
<chapter href="A.2+X000264.ditamap"/>
</bookmap>
- 每个手册的章节
<map id="X000264" xml:lang="de-DE">
<topicref href="A.2+X000265.ditamap"/>
</map>
- 目录 (.dita) 或 子章节 (.ditamap)
<map id="X000265" xml:lang="de-DE">
<topicref href="A.2+X000266.dita"/>
<topicref href="A.2+X000269.dita"/>
<topicref href="A.2+X000267.ditamap"/>
</map>
我的目标是一个完整的 xml-tree(你可以说是一个 'composed' 文档),所有文件都正确地嵌套到它们的引用给父节点中。
有没有一种简单的方法可以使用 <xsl:copy-of>
(可能有多个 'select' 选项来创建组合文档?
您需要根据参考资料编写模板,例如
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
复制不需要特殊处理的元素,然后
<xsl:template match="navref[@mapref]">
<xsl:apply-templates select="doc(@mapref)/node()"/>
</xsl:template>
<xsl:template match="chapter[@href] | topicref[@href]">
<xsl:apply-templates select="doc(@href)/node()"/>
</xsl:template>
<xsl:variable name="nested-tree">
<xsl:apply-templates select="/*"/>
</xsl:variable>
如果您想编写其他模板然后处理变量,使用模式来分隔处理步骤可能是有意义的:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* , node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="composed-doc">
<xsl:apply-templates select="/*" mode="compose"/>
</xsl:variable>
<xsl:template match="navref[@mapref]" mode="compose">
<xsl:apply-templates select="doc(@mapref)/node()" mode="compose"/>
</xsl:template>
<xsl:template match="chapter[@href] | topicref[@href]" mode="compose">
<xsl:apply-templates select="doc(@href)/node()" mode="compose"/>
</xsl:template>
</xsl:stylesheet>
如何为我所有引用的文档创建一个节点树并使用 XSLT 将其存储到一个变量中? (我使用 XSLT 2.0)
这是我的文件结构:
- Root 文档 .XML 包含所有特定语言的文档作为 ditamaps
<map> <navref mapref="de-DE/A.2+X000263.ditamap"/> <navref mapref="en-US/A.2+X000263.ditamap"/> <navref mapref="es-ES/A.2+X000263.ditamap"/> </map>
- 特定语言手册 (.ditamap) - 可能有多个文档
<bookmap id="X000263" xml:lang="de-DE"> <chapter href="A.2+X000264.ditamap"/> </bookmap>
- 每个手册的章节
<map id="X000264" xml:lang="de-DE"> <topicref href="A.2+X000265.ditamap"/> </map>
- 目录 (.dita) 或 子章节 (.ditamap)
<map id="X000265" xml:lang="de-DE"> <topicref href="A.2+X000266.dita"/> <topicref href="A.2+X000269.dita"/> <topicref href="A.2+X000267.ditamap"/> </map>
我的目标是一个完整的 xml-tree(你可以说是一个 'composed' 文档),所有文件都正确地嵌套到它们的引用给父节点中。
有没有一种简单的方法可以使用 <xsl:copy-of>
(可能有多个 'select' 选项来创建组合文档?
您需要根据参考资料编写模板,例如
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
复制不需要特殊处理的元素,然后
<xsl:template match="navref[@mapref]">
<xsl:apply-templates select="doc(@mapref)/node()"/>
</xsl:template>
<xsl:template match="chapter[@href] | topicref[@href]">
<xsl:apply-templates select="doc(@href)/node()"/>
</xsl:template>
<xsl:variable name="nested-tree">
<xsl:apply-templates select="/*"/>
</xsl:variable>
如果您想编写其他模板然后处理变量,使用模式来分隔处理步骤可能是有意义的:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* , node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="composed-doc">
<xsl:apply-templates select="/*" mode="compose"/>
</xsl:variable>
<xsl:template match="navref[@mapref]" mode="compose">
<xsl:apply-templates select="doc(@mapref)/node()" mode="compose"/>
</xsl:template>
<xsl:template match="chapter[@href] | topicref[@href]" mode="compose">
<xsl:apply-templates select="doc(@href)/node()" mode="compose"/>
</xsl:template>
</xsl:stylesheet>