You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XSL转换特定<p>标签为<note>及给指定文本加<apiname>标签?

可以用XSL实现这两个转换需求

1. 将以!!!开头的

标签转为标签

通过匹配<p>元素并检查其文本内容的开头标识,就能完成转换,还可以按需移除开头的!!!符号:

<!-- 匹配以!!!开头的p标签,转为note -->
<xsl:template match="p[starts-with(normalize-space(.), '!!!')]">
  <note>
    <!-- 移除开头的!!!及后续空格,保留剩余内容 -->
    <xsl:value-of select="substring-after(normalize-space(.), '!!!')"/>
  </note>
</xsl:template>

<!-- 保留其他普通p标签的原有结构 -->
<xsl:template match="p[not(starts-with(normalize-space(.), '!!!'))]">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

2. 给

标签内的'apiname'文本包裹标签

利用xsl:analyze-string匹配文本中的apiname字符串,替换为带标签的形式:

<!-- 处理p标签内的文本节点,匹配apiname并包裹标签 -->
<xsl:template match="p/text()">
  <xsl:analyze-string select="." regex="apiname">
    <xsl:matching-substring>
      <apiname><xsl:value-of select="."/></apiname>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

注意事项

把上述模板放到同一个XSL样式表中即可同时实现两个需求,若你的现有DITA转换已有默认的<p>处理模板,需要确保新模板的优先级更高(比如通过更具体的匹配条件),避免被原有模板覆盖。

内容的提问来源于stack exchange,提问作者Amy Higgins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 04:39:57