0

我有这个 xml 文件:

<w>
    <!-- many text nodes like below-->
    <text attr1="3" width="100">This is a sentence.</text>
    <text attr1="5" width="110">Another sentence, this time with a % in it.</text>
    <text attr1="9" width="40">Some text.</text>
    <text attr1="3" width="49">Other text.</text>
    <text attr1="1" width="90">Again some text</text>
    <!-- many text nodes like above-->
</w>

我想用 ' %' 折叠元素的所有后续节点,这些节点在其文本节点中具有 -width属性lower than 50 ,并用 包围整个组<tag1>,使其看起来像这样:

<w>
    <!-- many text nodes like below-->
    <text attr1="3" width="100">This is a sentence.</text>
    <text attr1="5" width="110">Another sentence, this time with a
        <tag1>% in it. Some text. Other text.</tag1>
    </text>
    <text attr1="1" width="90">Again some text</text>
    <!-- many text nodes like above-->
</w>

我已经用这个模板试过了:

<xsl:choose>
    <xsl:when test="contains(.,'%') and following-group|@width &lt;50">
        <!-- I cannot choose current-group(), it generates a output with no tag1's
            at all -->
        <tag1><xsl:value-of-select="."/></tag1>
    </xsl:when>
</xsl:choose>

但这只会将包含 s 的行放在%s 中<tag1>。我不明白为什么我不能选择我在测试中匹配的整个组。我也知道,即使它与整个事物相匹配,<tag1>s 也会将整行包含在 中%,但这是次要的。

4

1 回答 1

0

这是一个尝试解决它的示例,尽管我需要更多输入示例或更清晰和更详细的可能输入描述:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="w">
  <xsl:copy>
    <xsl:for-each-group select="*" group-starting-with="text[contains(., '%')]">
      <xsl:choose>
        <xsl:when test="not(self::text[contains(., '%')])">
          <xsl:apply-templates select="current-group()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="head" select="."/>
          <xsl:for-each-group select="current-group() except $head" group-adjacent="boolean(@width &lt; 50)">
            <xsl:choose>
              <xsl:when test="current-grouping-key()">  
                <xsl:element name="{name($head)}" namespace="{namespace-uri($head)}">
                  <xsl:copy-of select="@*"/>
                  <xsl:analyze-string select="$head" regex="%.*$">
                    <xsl:matching-substring>
                      <tag1>
                        <xsl:value-of select="., current-group()/node()/string()"/>
                      </tag1>
                    </xsl:matching-substring>
                    <xsl:non-matching-substring>
                      <xsl:value-of select="."/>
                    </xsl:non-matching-substring>
                  </xsl:analyze-string>
                </xsl:element>
              </xsl:when>
              <xsl:otherwise>
                <xsl:apply-templates select="current-group()"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each-group>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

使用该样式表 Saxon 9 转换输入样本

<w>
    <text>foo</text>
    <text>bar</text>
    <text attr1="3" width="100">This is a sentence.</text>
    <text attr1="5" width="110">Another sentence, this time with a % in it.</text>
    <text attr1="9" width="40">Some text.</text>
    <text attr1="3" width="49">Other text.</text>
    <text attr1="1" width="90">Again some text</text>
    <text>foo bar</text>
    <text>foo baz</text>
</w>

成以下结果:

<w>
   <text>foo</text>
   <text>bar</text>
   <text attr1="3" width="100">This is a sentence.</text>
   <text attr1="9" width="40">Another sentence, this time with a <tag1>% in it. Some text. Other text.</tag1>
   </text>
   <text attr1="1" width="90">Again some text</text>
   <text>foo bar</text>
   <text>foo baz</text>
</w>
于 2013-07-17T11:51:34.550 回答