Xsl 1.0 Group by小写

时间:2015-05-18 12:01:45

标签: html xml xslt

有没有办法按不区分大小写的值进行分组。 我有一个类似

的XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc>
  <Item id="12" type="data" value="Apple" /> 
  <Item id="13" type="data" value="apple" /> 
  <Item id="14" type="data" value="APPLE" /> 
  <Item id="11" type="data" value="car" /> 
  <Item id="10" type="data" value="CAR" /> 
  <Item id="9" type="data" value="Car" /> 
  <Item id="8" type="data" value="Tomato" /> 
  <Item id="7" type="data" value="tomato" /> 
  <Item id="6" type="data" value="TOMATO" /> 
  <Item id="24" type="data" value="TOMATOES" /> 
  <Item id="31" type="data" value="peach" /> 
  <Item id="28" type="data" value="peach" /> 
  <Item id="56" type="data" value="peach" /> 
  <Item id="62" type="data" value="Peach" /> 
  <Item id="78" type="data" value="PEACH" /> 
</doc>

我想输出如下:

  • apple:3
  • car:3
  • 番茄:3
  • 西红柿:1
  • 桃子:5

我试图应用这样的样式表,但它不起作用:

 <?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
  <xsl:output method="html"/>
  <xsl:strip-space elements="*"/>
  <xsl:key name="tag" match="*" use="translate(@Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
  <xsl:template match="doc">
   <html>
    <body>
     <xsl:apply-templates/>
    </body>
   </html>
  </xsl:template>
  <xsl:template match="*">
   <ul>
    <xsl:for-each select="*[count(. | key('tag', translate(@Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'))[1]) = 1]" >
   <xsl:sort select="translate(@Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
   <li>
     <xsl:variable name="cv" select="translate(@Value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
     <xsl:value-of select="count(*[$cv=current()/@Value]/@Id)"/>
   </li>
 </xsl:for-each>
 </ul>
</xsl:template>
</xsl:stylesheet>

我是否必须将每个节点转换为小写,然后分组?

2 个答案:

答案 0 :(得分:0)

我会将key限制为Item元素,然后确保Muenchian分组和排序在正确的模板中完成:

<?xml version="1.0"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
  <xsl:output method="html"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  <xsl:variable name="lc" select="'abcdefghijklmnopqrstuvwxyz'"/>

  <xsl:key name="tag" match="Item" use="translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>

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

  <xsl:template match="doc">
      <ul>
          <xsl:apply-templates select="Item[generate-id() = generate-id(key('tag', translate(@value, $uc, $lc))[1])]">
              <xsl:sort select="translate(@value, $uc, $lc)"/>
          </xsl:apply-templates>
      </ul>
  </xsl:template>

  <xsl:template match="Item">
   <li>
     <xsl:value-of select="concat(translate(@value, $uc, $lc), ': ', count(key('tag', translate(@value, $uc, $lc))))"/>
   </li>
</xsl:template>
</xsl:stylesheet>

但结果是

<html>
   <body>
      <ul>
         <li>apple: 3</li>
         <li>car: 3</li>
         <li>peach: 5</li>
         <li>tomato: 3</li>
         <li>tomatoes: 1</li>
      </ul>
   </body>
</html>

然后,我不确定你的帖子为什么peach作为最后一项。

答案 1 :(得分:0)

以这种方式试试吗?

XSLT 1.0

<xsl:stylesheet version="1.1" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="tag" match="Item" use="translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>

<xsl:template match="/doc">
    <html>
        <body>
            <ul>
                <xsl:for-each select="Item[count(. | key('tag', translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'))[1]) = 1]" >
                    <li>
                         <xsl:variable name="cv" select="translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
                        <xsl:value-of select="concat($cv, ': ', count(key('tag', $cv)))"/>
                    </li>
                </xsl:for-each>
            </ul>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

注意:正如您所注意到的,XML区分大小写 - @Value与名为value的属性不匹配。