Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xslt中,将标记放在两个常量词之间的空格中_Xslt - Fatal编程技术网

在xslt中,将标记放在两个常量词之间的空格中

在xslt中,将标记放在两个常量词之间的空格中,xslt,Xslt,我想选择两个特定单词之间的空格,并在那里放置一些标记。我正在使用XSLT2.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&g

我想选择两个特定单词之间的空格,并在那里放置一些标记。我正在使用XSLT2.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>
试用代码无效。

从标识模板开始。由于模板优先级的详细信息, 应将其放置在第二个模板之前,请参见下文

然后脚本应该包含与文本节点匹配的模板,包括 xsl:分析字符串。 regex属性应该包含两个需要的字符串作为捕获 组之间有一个空格

里面应该是:

xsl:匹配子字符串打印: 第一组用正则表达式捕获, 元素或任何你想要的元素, 第2组。 xsl:不匹配的子字符串,只是复制不匹配的文本。 请注意,第二个需要的字符串包含括号,它们是 特殊的正则表达式字符,所以要从字面上对待它们,它们应该被转义 用\

因此,整个脚本可以如下所示:

<?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>
请注意,我添加了过滤器 不必要的空间

<chapter match="[starts-with('command')]//text()[ends-with('(ever publish)')]/text()">
  <t/>
</chapter>
<?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>