基于属性值为同一模板创建独立模式,实现单XSL双输出
Solution for Attribute-Based Modes & Dual Output in XSLT
I've put together a single XSLT file that handles both your requirements: creating separate processing modes based on attribute values, and generating two distinct output types depending on the root element's outputclass attribute. Here's how it works:
Core Approach
We'll use XSLT modes to split processing logic while reusing shared templates. The root element's outputclass will determine which mode is triggered, including special handling for your rare "inter" scenario that references an external HTML file.
XSLT Implementation
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Default mode for regular output --> <xsl:template match="/" mode="regular"> <regular-output> <xsl:apply-templates/> </regular-output> </xsl:template> <!-- Mode for "inter" output class (rare scenario) --> <xsl:template match="/" mode="inter"> <inter-output> <!-- Process the core topic elements --> <xsl:apply-templates select="topic"/> <!-- Integrate content from the referenced inter.html file --> <xsl:copy-of select="document('inter.html')//*"/> </inter-output> </xsl:template> <!-- Entry point: switch mode based on root outputclass --> <xsl:template match="/"> <xsl:choose> <xsl:when test="/topic/@outputclass = 'inter'"> <xsl:apply-templates select="." mode="inter"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="regular"/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- Reusable template for title (works in all modes) --> <xsl:template match="title" mode="#all"> <formatted-title> <xsl:value-of select="."/> </formatted-title> </xsl:template> <!-- Specialized xref handling for inter mode --> <xsl:template match="xref[@format='html' and @scope='external']" mode="inter"> <external-reference href="{@href}"> <xsl:comment>Linked content from inter.html integrated above</xsl:comment> </external-reference> </xsl:template> <!-- Default xref handling for regular mode --> <xsl:template match="xref" mode="regular"> <internal-reference> <xsl:value-of select="@href"/> </internal-reference> </xsl:template> </xsl:stylesheet>
Key Details
- Mode Reuse: The
titletemplate usesmode="#all"to apply to both processing modes, keeping code DRY. - Conditional Trigger: The root template checks the
outputclassattribute to switch between regular and inter-specific logic. - External HTML Integration: The
document()function pulls content frominter.html(ensure your XSLT processor allows external document access; adjust theselectexpression if you only need a specific section of the HTML). - Attribute-Specific Matching: The
xreftemplates target the exact attribute values from your rare scenario to handle external links correctly.
Example Input XML (Rare Scenario)
<topic outputclass="inter"> <title class="- topic/title ">Inter Topic Title</title> <body class="- topic/body "> <bodydiv class="- topic/bodydiv "> <xref class="- topic/xref " format="html" href="inter.html" scope="external"/> </bodydiv> </body> </topic>
内容的提问来源于stack exchange,提问作者Aakash Kumar
相关产品推荐
相关产品推荐

