问题
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:
- 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>
- Language specific manuals (.ditamap) - multiple documents possible
<bookmap id="X000263" xml:lang="de-DE"> <chapter href="A.2+X000264.ditamap"/> </bookmap>
- Chapters for each manual
<map id="X000264" xml:lang="de-DE"> <topicref href="A.2+X000265.ditamap"/> </map>
- 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