2

xsl をグループ化するために使用している次の xsl テンプレートがあります。私が抱えている問題は、@Title を大文字にする必要があることです。現在、グループ化では大文字と小文字が別々のグループとして認識されているためです。

  <xsl:key name="rows-by-title" match="Row" use="substring(@Title,1,1)" />

    <xsl:template name="Meunchian" match="/dsQueryResponse/Rows">


            <xsl:for-each select="Row[count(. | key('rows-by-title', substring(@Title,1,1))[1]) = 1]">


                <xsl:sort select="substring(@Title,1,1)" />

                <p></p><xsl:value-of select="substring(@Title,1,1)" /><br />
                    <xsl:for-each select="key('rows-by-title', substring(@Title,1,1))">
                        <xsl:value-of select="@Title" /><br/>
                    </xsl:for-each>

            </xsl:for-each>

    </xsl:template>

call-template を使用して変数を設定しようとしましたが、xsl はこれを気に入らないようです:

 <xsl:key name="rows-by-title" match="Row" use="substring(@Title,1,1)" />

    <xsl:template name="Meunchian" match="/dsQueryResponse/Rows">



            <xsl:for-each select="Row[count(. | key('rows-by-title', substring(@Title,1,1))[1]) = 1]">

    <xsl:variable name="myTitle">

          <xsl:call-template name="to-upper">
            <xsl:with-param name="text">
                <xsl:value-of select="@Title"/>
            </xsl:with-param>
          </xsl:call-template>

    </xsl:variable>

                <p></p><xsl:value-of select="$myTitle" /><br />
                    <xsl:for-each select="key('rows-by-title', substring(@Title,1,1))">
                        <xsl:value-of select="@Title" /><br/>
                    </xsl:for-each>

            </xsl:for-each>

    </xsl:template>

私が達成しようとしているのは、メウンチアンのグループ化ですが、大文字と小文字を区別しません-これが理にかなっていることを願っています!

キーラン

4

1 に答える 1

3

小文字を大文字に変換する方法は、XPath の translate() 関数を使用することです。

それを使用して、目的の変換を表現する 1 つの方法は次のとおりです。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml"/>

 <xsl:variable name="vLower" select=
  "'abcdefghijklmnopqrstuvwxyz'"
  />

 <xsl:variable name="vUpper" select=
  "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
  />

 <xsl:key name="rows-by-title" match="Row" use=
 "translate(substring(@Title,1,1),
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
            )" />

    <xsl:template match="/">
     <html>
      <xsl:apply-templates select="*/*"/>
     </html>
    </xsl:template>

    <xsl:template name="Meunchian" match="/dsQueryResponse/Rows">
        <xsl:for-each select=
          "Row[generate-id()
              =
               generate-id(key('rows-by-title',
                                translate(substring(@Title,1,1),
                                          $vLower,
                                          $vUpper)
                                )[1]
                       )
               ]">
            <xsl:sort select="translate(substring(@Title,1,1),
                                              $vLower,
                                              $vUpper)" />
            <p></p>
            <xsl:value-of select="translate(substring(@Title,1,1),
                                                  $vLower,
                                                  $vUpper)" />
            <br />
            <xsl:for-each select=
                             "key('rows-by-title', 
                                   translate(substring(@Title,1,1),
                                     $vLower,
                                     $vUpper)">

                <xsl:value-of select="@Title" />
                <br/>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

次の XML ドキュメントに適用した場合:

<dsQueryResponse>
    <Rows>
        <Row Title="Agenda" />
        <Row Title="Policy" />
        <Row Title="policy" />
        <Row Title="Report" />
        <Row Title="report" />
        <Row Title="Test2" />
        <Row Title="test1" />
        <Row Title="Boo" />
        <Row Title="foo" />
    </Rows>
</dsQueryResponse>

それは望ましい結果を生成します

<html>
    <p/>A
    <br/>Agenda
    <br/>
    <p/>B
    <br/>Boo
    <br/>
    <p/>F
    <br/>foo
    <br/>
    <p/>P
    <br/>Policy
    <br/>policy
    <br/>
    <p/>R
    <br/>Report
    <br/>report
    <br/>
    <p/>T
    <br/>Test2
    <br/>test1
    <br/>
</html>

XPath 2.0 では、upper-case() 関数を使用して小文字を大文字に変換します。

また、 XSLT 2.0 でのグループ化は、 命令を使用してより適切に表現できます<xsl:for-each-group>

于 2008-12-24T05:22:11.260 に答える