Create Composed Documents with XSLT (@href)

会有一股神秘感。 提交于 2019-12-24 03:32:32

问题


How can I create a node-tree for all my referenced documents and store it into a variable with XSLT? (I´m using XSLT 2.0)

This is my file structure:

  1. Root Document .XML contains all language specific documents as 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>
  2. Language specific manuals (.ditamap) - multiple documents possible
    <bookmap id="X000263" xml:lang="de-DE"> <chapter href="A.2+X000264.ditamap"/> </bookmap>
  3. Chapters for each manual
    <map id="X000264" xml:lang="de-DE"> <topicref href="A.2+X000265.ditamap"/> </map>
  4. Contents (.dita) or SUB-Chapters (.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>

I´m aiming for a complete xml-tree (you could say a 'composed' document) with all files correctly nested into their reference giving parent nodes.

Is there an easy way to create a composed document with <xsl:copy-of> (maybe with mutliple 'select' options?


回答1:


You would need to write templates following the references e.g.

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

to copy elements that don't need special treatment, then

<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>

If you want to write other templates then to process the variable it might make sense to use modes to separate processing steps:

<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>


来源:https://stackoverflow.com/questions/31986773/create-composed-documents-with-xslt-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!