将标签放在XSLT中两个常数单词之间的空间上



我想选择在特定单词之间的空间,然后在那里放一些标签。我正在使用XSLT 2.0

<chapter>
  <p type="Entry"><doc refType="anchor">
    <t/>Command K (ever publish)<t/></doc><ref format="Page Number" refType="anchor" refId="sec-sec_G"/>80
  </p>
</chapter>

预期输出:

<chapter>
  <p type="Entry"><doc refType="anchor">
    <t/>Command K<t/>(ever publish)<t/></doc><ref format="Page Number" refType="anchor" refId="sec-sec_G"/>80
  </p>
</chapter>

我的预期输出是,将<t/>标签放在(ever publish)Command K字符串之间。(ever publish)Command是常数。角色K可以更改。

尝试的代码:

<chapter match="[starts-with('command')]//text()[ends-with('(ever publish)')]/text()">
  <t/>
</chapter>

尝试的代码无法正常工作。

身份模板开始。由于模板的优先级详细信息,它应该放在第二个模板之前(见下文(。

然后,您的脚本应包含一个模板匹配 text((节点,包括 XSL:分析 - 弦乐 REGEX 属性应包含两个"想要"字符串作为捕获它们之间有空间的组。

内部应该是:

  • XSL:匹配 - substring 打印:
    • 第1组(用正则捕获(,
    • &lt; t/&gt; element(或您在这里想要的任何东西(,
    • 第2组。
  • xsl:非匹配 - substring ,只需复制非匹配的文本。

请注意,第二个"想要"字符串包含括号,特殊的正则chars,因此要对它们进行实际处理,应将它们逃脱使用

因此,整个脚本可以看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:analyze-string select="." regex="(Command K) ((ever publish))">
      <xsl:matching-substring>
        <xsl:value-of select="regex-group(1)"/>
        <t/>
        <xsl:value-of select="regex-group(2)"/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>
</xsl:stylesheet>

请注意,我添加了<xsl:strip-space elements="*"/>以过滤出来不必要的空间。

最新更新