【问题标题】:Create Composed Documents with XSLT (@href)使用 XSLT (@href) 创建组合文档
【发布时间】:2015-08-13 11:21:44
【问题描述】:

如何为所有引用的文档创建节点树并使用 XSLT 将其存储到变量中? (我正在使用 XSLT 2.0)

这是我的文件结构:

  1. Root 文档 .XML 包含所有语言特定的文档作为 ditamap
    <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>
  2. 特定语言的手册 (.ditamap) - 可能有多个文档
    <bookmap id="X000263" xml:lang="de-DE"> <chapter href="A.2+X000264.ditamap"/> </bookmap>
  3. 每本手册的章节
    <map id="X000264" xml:lang="de-DE"> <topicref href="A.2+X000265.ditamap"/> </map>
  4. 内容 (.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 树(您可以说是一个“组合”文档),其中所有文件都正确嵌套在它们的引用中,并提供父节点。

有没有一种简单的方法可以使用&lt;xsl:copy-of&gt; 创建一个组合文档(可能有多个“选择”选项?

【问题讨论】:

    标签: xml xslt xslt-2.0 dita


    【解决方案1】:

    您需要在参考之后编写模板,例如

    <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 转换作为一个自己的过程 - 我的意思是不要在我的样式表中包含我所做的所有其他事情;我怎么能用模式做到这一点?如果您能在回答中描述这一点,我将不胜感激。
    • @garlicDoge,如果您只是想要一个遵循所有引用并输出组合文档的样式表,那么您不需要任何模式,我已经编辑了答案并提供了一个使用 sn- 的示例样式表ps 之前建议过。
    • 我需要将组合文档存储在一个变量中。但是没有设置模式,我放入变量中的一些节点已经转换为例如&lt;fo:block 元素。
    • @garlicDoge,我已经编辑了答案以使用mode="compose" 将组合文档存储到变量中,并将该处理步骤与其他步骤分开。如果您不需要第一个模板(身份转换)进行其他处理步骤,那么也只需在此处使用&lt;xsl:template match="@* | node()" mode="compose"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@* , node()" mode="compose"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt;
    • 非常感谢!这正是我所需要的。我也开始使用不同的模式。希望我能给 +100 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 2020-09-17
    相关资源
    最近更新 更多