1

在使用 xsl 对类似节点进行分组时,我遇到了以下问题:

输入:

<?xml version="1.0" encoding="UTF-8"?>
<concept id="ads">
    <p>para1 content goes here</p>
    <Bullet>1</Bullet>
    <Bullet>2</Bullet>
    <Bullet>3</Bullet>
    <p>para2 content goes here</p>
    <Bullet>4</Bullet>
    <Bullet>5</Bullet>
    <p>para2 content goes here</p>
</concept>

输出应该是:

<?xml version="1.0" encoding="UTF-8"?>
<concept id="ads">
    <p>para1 content goes here</p>
    <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    </ul>
    <p>para2 content goes here</p>
    <ul>
    <li>4</li>
    <li>5</li>
    </ul>
    <p>para2 content goes here</p>
</concept>

所有没有 imideate 前面的兄弟作为“Bullet”的“Bullet”元素都应该包含在“UL”和“Li”元素中。

我正在尝试这样的事情,但无法获得结果:

<xsl:for-each select="Bullet[not(preceding-sibling::Bullet)]">
<ul>
<xsl:copy-of select="."/>
<xsl:variable name="current-name" select="generate-id(.)"/>
<xsl:for-each select="following-sibling::*[Bullet][generate-id(preceding-sibling::*[Bullet]) = $current-name]">
<xsl:copy-of select="."/>
</xsl:for-each>
</ul>
</xsl:for-each>

请帮忙。

---相关问题----

这适用于给定的 xml 输入,但是 1. 在我的实际 xml 中,这些<Bullet>标签可以出现在概念节点下的任何位置,因此<p>在这种情况下与元素分组不起作用。

  1. 目前,我正在处理除“Bullet”节点之外的所有节点,因此我只需要匹配<Bullet>具有紧随其后的兄弟节点的节点,<Bullet>并将序列包装在<ul>和 each <Bullet>in 中<li>。我当前的上下文节点是<concept>.

  2. 当前样式表如下:

    应该在与输入相同的位置呈现的节点。-->

实际的 XML 模式:

<?xml version="1.0" encoding="UTF-8"?> 
<concept id="ads"> 
    <p>para1 content goes here</p> 
    <Body-text>Body1 content goes here</Body-text>
    <Bullet>1</Bullet> 
    <Bullet>2</Bullet> 
    <Bullet>3</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p> 
    <Bullet>4</Bullet> 
    <Bullet>5</Bullet> 
    <p>para3 content goes here</p>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
</concept>
<concept id="ads"> 
    <p>para1 content goes here</p> 
    <Body-text>Body1 content goes here</Body-text>
    <Bullet>1</Bullet> 
    <Bullet>2</Bullet> 
    <Bullet>3</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p> 
    <Bullet>4</Bullet> 
    <Bullet>5</Bullet> 
    <p>para3 content goes here</p>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
    <Body-text>Body2 content goes here</Body-text>
    <Bullet>6</Bullet> 
    <Bullet>7</Bullet> 
</concept>
4

2 回答 2

1

这个样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()[1]|@*"/>
        </xsl:copy>
        <xsl:apply-templates select="following-sibling::node()[1]"/>
    </xsl:template>
    <xsl:template match="Bullet[preceding-sibling::node()[1]
                               [not(self::Bullet)]]">
        <ul>
            <xsl:call-template name="makeLi"/>
        </ul>
        <xsl:apply-templates select="following-sibling::node()
                                                     [not(self::Bullet)][1]"/>
    </xsl:template>
    <xsl:template match="Bullet" name="makeLi">
        <li>
            <xsl:value-of select="."/>
        </li>
        <xsl:apply-templates select="following-sibling::node()[1]
                                                              [self::Bullet]"/>
    </xsl:template>
</xsl:stylesheet>

编辑:只需将以下应用“first Bullet”规则更改为 first next 而Bullet不是p.

输出(用根元素包装你的输入以形成良好的格式):

<concept id="ads">
    <p>para1 content goes here</p>
    <Body-text>Body1 content goes here</Body-text>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p>
    <ul>
        <li>4</li>
        <li>5</li>
    </ul>
    <p>para3 content goes here</p>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
</concept>
<concept id="ads">
    <p>para1 content goes here</p>
    <Body-text>Body1 content goes here</Body-text>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <p>para2 content goes here</p>
    <ul>
        <li>4</li>
        <li>5</li>
    </ul>
    <p>para3 content goes here</p>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
    <Body-text>Body2 content goes here</Body-text>
    <ul>
        <li>6</li>
        <li>7</li>
    </ul>
</concept>

注意:细粒度遍历。

通过分组,这个样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="concept">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each-group select="*" 
                                group-adjacent="boolean(self::Bullet)">
                <xsl:choose>
                    <xsl:when test="current-grouping-key()">
                        <ul>
                            <xsl:apply-templates select="current-group()"/>
                        </ul>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Bullet">
        <li>
            <xsl:value-of select="."/>
        </li>
    </xsl:template>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

编辑:使用group-adjacent.

于 2010-10-04T14:52:03.910 回答
0

这个 XSLT 2.0 转换

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/*">
      <xsl:copy>
       <xsl:copy-of select="@*"/>
        <xsl:for-each-group select="*" group-starting-with="p">
          <xsl:copy-of select="current-group()[1]"/>
           <xsl:if test="current-group()[2]">
               <ul>
                <xsl:apply-templates select="current-group()[position() gt 1]"/>
              </ul>
          </xsl:if>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="Bullet">
      <li><xsl:value-of select="."/></li>
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<concept id="ads">
    <p>para1 content goes here</p>
    <Bullet>1</Bullet>
    <Bullet>2</Bullet>
    <Bullet>3</Bullet>
    <p>para2 content goes here</p>
    <Bullet>4</Bullet>
    <Bullet>5</Bullet>
    <p>para2 content goes here</p>
</concept>

产生想要的正确结果

<concept id="ads">
   <p>para1 content goes here</p>
   <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
   </ul>
   <p>para2 content goes here</p>
   <ul>
      <li>4</li>
      <li>5</li>
   </ul>
   <p>para2 content goes here</p>
</concept>

请注意<xsl:for-each-group>withgroup-starting-with属性和current-group()函数的使用。

于 2010-10-04T18:44:30.200 回答