将以下兄弟的属性分组为具有条件的元素

时间:2013-07-17 11:07:00

标签: xslt-2.0

我有这个xml-File:

<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进入其文本节点并用{围绕整个组{1}},所以它看起来像这样:

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

1 个答案:

答案 0 :(得分: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>