如何从XML属性获取XSLT参数值并实现产品类型过滤
实现基于产品类型参数的DITA Map到HTML过滤转换
1. 定义XSLT过滤参数
首先在XSLT文件顶部用<xsl:param>声明产品类型参数,支持默认值或从XML属性自动读取:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dita="http://dita.oasis-open.org/architecture/2005/"> <!-- 两种参数赋值方式二选一: --> <!-- 方式1:外部传参优先,默认值设为通用产品 --> <xsl:param name="target-product" select="'general'"/> <!-- 方式2:自动读取DITA Map根元素的product-type属性作为参数值 --> <!-- <xsl:param name="target-product" select="/dita:map/@product-type"/> -->
2. 从XML属性绑定参数值
如果选择上述方式2,参数会自动获取DITA Map根节点的product-type属性值,无需手动传参。如果需要外部覆盖这个值,调用XSLT工具时传入参数即可(比如用Saxon命令行:java -jar saxon9he.jar -s:map.ditamap -xsl:filter.xsl -o:output.html target-product="ProductX")。
3. 编写过滤转换逻辑
根据你的需求,分两种场景处理topicref的过滤:
场景1:过滤topicref自身带产品属性的节点
如果topicref直接带有product属性,在模板中添加条件判断,只处理匹配参数值的节点:
<xsl:template match="dita:topicref"> <!-- 保留匹配目标产品的节点,以及无产品属性的通用节点 --> <xsl:if test="@product = $target-product or not(@product)"> <!-- 这里写你的HTML转换逻辑 --> <div class="topic-item"> <h3><xsl:value-of select="@navtitle"/></h3> <!-- 加载并渲染引用的topic内容 --> <xsl:apply-templates select="document(@href)/dita:topic/dita:body"/> </div> </xsl:if> </xsl:template>
场景2:过滤引用topic文件中的产品属性
如果产品类型属性在被引用的.dita文件里,先加载文档再判断:
<xsl:template match="dita:topicref"> <xsl:variable name="topic-content" select="document(@href)"/> <!-- 匹配topic根节点的product-type属性 --> <xsl:if test="$topic-content/dita:topic/@product-type = $target-product or not($topic-content/dita:topic/@product-type)"> <div class="topic-item"> <h3><xsl:value-of select="$topic-content/dita:topic/@title"/></h3> <xsl:apply-templates select="$topic-content/dita:topic/dita:body"/> </div> </xsl:if> </xsl:template>
完整示例代码
示例DITA Map(map.ditamap)
<map xmlns="http://dita.oasis-open.org/architecture/2005/" product-type="ProductA"> <topicref href="common-topic.dita" navtitle="通用操作指南"/> <topicref href="productA-setup.dita" navtitle="ProductA安装配置" product="ProductA"/> <topicref href="productB-feature.dita" navtitle="ProductB专属功能" product="ProductB"/> <!-- 其余6个topicref --> </map>
对应XSLT(filter.xsl)
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dita="http://dita.oasis-open.org/architecture/2005/" exclude-result-prefixes="dita"> <xsl:param name="target-product" select="/dita:map/@product-type"/> <xsl:template match="/"> <html> <head> <title><xsl:value-of select="/dita:map/@title"/> - 产品专属文档</title> </head> <body> <h1><xsl:value-of select="/dita:map/@title"/></h1> <xsl:apply-templates select="dita:map/dita:topicref"/> </body> </html> </xsl:template> <xsl:template match="dita:topicref"> <xsl:if test="@product = $target-product or not(@product)"> <div class="topic-section"> <h2><xsl:value-of select="@navtitle"/></h2> <xsl:apply-templates select="document(@href)/dita:topic/dita:body"/> </div> </xsl:if> </xsl:template> <xsl:template match="dita:p"> <p><xsl:value-of select="."/></p> </xsl:template> </xsl:stylesheet>
转换后会自动过滤掉与target-product不匹配的topicref,只生成目标产品的HTML内容。
内容的提问来源于stack exchange,提问作者Imp
相关产品推荐
相关产品推荐

