English 中文(简体)
合并页数
原标题:Combining entries to page numbers

我有以下指数条目:

<entry name="a" page="1" />
<entry name="b" page="3" />
<entry name="b" page="4" />
<entry name="b" page="6" />
<entry name="c" page="7" />

我现在想得到这样的东西。

<index name="a" pages="1 />
<index name="b" pages="3-4, 6" />
<index name="c" pages="7" />

是否有人准备利用这一职能?

我正在考虑两种通行证办法,首先以指数条目的形式,如格式。

<index name="a" pages="1 />
<index name="b" pages="3 4 6" />
<index name="c" pages="7" />

然后将“3 4 6”改为“3-4 6”。 第一步容易:

<xsl:for-each-group select="index" group-adjacent="@name">
  <xsl:element name="index">
    <xsl:attribute name="name" select="@name"/>
    <xsl:attribute name="pages" select="current-group()/@page"/>
  </xsl:element>
</xsl:for-each-group>

(这里的属性页当然可能有所不同)

现在是困难的一步,我应如何通过名单来回避? 必须对以下几页(我可以将网页按顺序排列)为目前的“@page+1”进行核对。

最佳回答

Here is a sample:

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

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="entries">
    <xsl:for-each-group select="entry" group-by="@name">
      <index name="{current-grouping-key()}">
        <xsl:attribute name="pages">
          <xsl:for-each-group select="current-group()/xs:integer(@page)" group-by="position() - .">
            <xsl:if test="position() gt 1">
              <xsl:text>, </xsl:text>
            </xsl:if>
            <xsl:value-of select="if (current-group()[2]) 
                                  then (current-group()[1], current-group()[last()])
                                  else ." 
                          separator="-"/>
          </xsl:for-each-group>
        </xsl:attribute>
      </index>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

投入

<entries>
<entry name="a" page="1" />
<entry name="b" page="3" />
<entry name="b" page="4" />
<entry name="b" page="6" />
<entry name="c" page="7" />
</entries>

我得到结果。

<index name="a" pages="1"/>
<index name="b" pages="3-4, 6"/>
<index name="c" pages="7"/>
问题回答

www.un.org/spanish/ecosoc 另一个解决办法是:

<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 omit-xml-declaration="yes" indent="yes"/>

      <xsl:template match="entries">
        <xsl:for-each-group select="entry" group-by="@name">
          <index name="{current-grouping-key()}">
            <xsl:attribute name="pages">
              <xsl:variable name="vAtribVals" as="xs:integer*">
                <xsl:perform-sort select="current-group()/@page/xs:integer(.)">
                  <xsl:sort data-type="number"/>
                </xsl:perform-sort>
              </xsl:variable>

              <xsl:sequence select=
                "$vAtribVals[1],
                 for $k in 2 to count($vAtribVals)
                   return
                     if($vAtribVals[$k] - $vAtribVals[$k -1] ne 1)
                       then concat( , , $vAtribVals[$k])
                       else
                         if($vAtribVals[$k+1] - $vAtribVals[$k] ne 1)
                           then concat( - , $vAtribVals[$k])
                           else()
                 "/>
            </xsl:attribute>
          </index>
        </xsl:for-each-group>
      </xsl:template>
</xsl:stylesheet>

www.un.org/spanish/ecosoc 在以下XML文件上适用这一转变:

<entries>
    <entry name="a" page="1" />
    <entry name="b" page="3" />
    <entry name="b" page="4" />
    <entry name="b" page="6" />
    <entry name="c" page="7" />
</entries>

<t, 正确结果::

<index name="a" pages="1"/>
<index name="b" pages="3-4,6"/>
<index name="c" pages="7"/>

  1. page属性在进一步处理之前进行分类,即便在源XML文件中没有按顺序排列的<代码>enter/code>要素,也可进行正确的处理。

  2. 处理方式取决于分类价值和地位之间的关系,因此,在其他情况下,属性价值为t integers。





相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签