for-each并使用xslt对xml进行排序和分组

时间:2013-03-18 19:49:03

标签: xslt xslt-1.0 xslt-grouping

我有一个XML,如下所示。对于每个测试,我需要对猫,标题和组进行排序。 对于cat值abc,我需要获得标题和设置值。 任何设置值都需要按升序在集合节点内添加标题值。在这里,我对cat值进行了硬编码。但在我的场景中,cat值来自外部源。

<?xml version="1.0" encoding="utf-8" ?>
<compound>
  <test>
    <title>k</title>
    <cetval>
      <cat>abc</cat>
      <cat>fcg</cat>
    </cetval>
    <value>1</value>
    <set>
      <color>blue</color>
    </set>
  </test>
  <test>
    <title>p</title>
    <cetval>
      <cat>fcg</cat>
      <cat>klm</cat>
    </cetval>

    <value>10</value>
    <set>
      <color>pink</color>
    </set>
  </test>
  <test>
    <title>j</title>
    <cetval>
      <cat>fcg</cat>
      <cat>klm</cat>
      <cat>abc</cat>
    </cetval>
    <value>484</value>
    <set>
      <color>yellow</color>
    </set>
  </test>
  <test>
    <title>v</title>
    <cetval>
      <cat>dji</cat>
      <cat>kfjsdlk</cat>
    </cetval>
    <value>93</value>

  </test>
  <test>
    <title>r</title>
    <cetval>
      <cat>deiu</cat>
      <cat>kfjdf</cat>
      <cat>abc</cat>
    </cetval>
    <value>10</value>
    <set>
      <color>blue</color>
    </set>
    <test>
      <title>i</title>
      <cetval>
        <cat>fcg</cat>
        <cat>klm</cat>
        <cat>abc</cat>
      </cetval>

      <value>10</value>

    </test>
    <test>
      <title>w</title>
      <cetval>
        <cat>diweif</cat>
        <cat>fjf</cat>
        <cat>abc</cat>
      </cetval>
      <value>10</value>
      <set>
        <color>yellow</color>
      </set>
    </test>
  </test>

</compound>    

我需要输出如下:

<?xml version="1.0" encoding="utf-8" ?>
<compound>
  <cat>abc</cat>
  <set>
    <color>blue</color>
    <title>k</title>
    <title>r</title>
  </set>
  <set>
    <color>yellow</color>
    <title>j</title>
    <title>w</title>
  </set>
  <title>i</title>


</compound>

我的转换如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <xsl:for-each select="compound/test">
        <xsl:sort select="./cetval/cat" order="ascending"/>
        <xsl:sort select="./title" order="ascending"/>
        <xsl:for-each select="./cetval/cat">
          <xsl:choose>
            <xsl:when test="./cat = 'abc' > 0">

              <xsl:value-of select="ancestor::test/title"/>

            </xsl:when>
          </xsl:choose>
        </xsl:for-each>

        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

任何人都可以建议我如何生成所需的输出吗?

2 个答案:

答案 0 :(得分:2)

好的,我想我已经理解了你的要求。怎么回事:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
  <xsl:key name="kColor" match="test" use="set/color" />
  <xsl:param name="catValue" select="'abc'" />

  <xsl:template match="/*">
    <xsl:copy>
      <cat>
        <xsl:value-of select ="$catValue"/>
      </cat>
      <xsl:apply-templates 
        select="//test[generate-id() = 
                       generate-id(
                            key('kColor', set/color)
                                  [cetval/cat = $catValue][1]
                                  )
                      ]" />
      <xsl:apply-templates 
        select="//test[cetval/cat = $catValue and not(set/color)]
                      /title">
        <xsl:sort select="."/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="test">
    <set>
      <xsl:apply-templates select="set/color" />
      <xsl:apply-templates
        select="key('kColor', set/color)[cetval/cat = $catValue]/title">
        <xsl:sort select="." />
      </xsl:apply-templates>
    </set>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

此处$catValue被指定为参数,因此您可以从外部传递不同的值。在样本XML上运行此操作时,结果为:

<compound>
  <cat>abc</cat>
  <set>
    <color>blue</color>
    <title>k</title>
    <title>r</title>
  </set>
  <set>
    <color>yellow</color>
    <title>j</title>
    <title>w</title>
  </set>
  <title>i</title>
</compound>

答案 1 :(得分:0)

更短更简单

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

 <xsl:key name="kTestByCat" match="test" use="cetval/cat"/>
 <xsl:key name="kTestByColor" match="test[cetval/cat='abc']"
      use="set/color"/>

 <xsl:template match="/">
  <compound>
    <cat>abc</cat>
      <xsl:apply-templates select=
      "/*/*[generate-id()
           =
            generate-id(key('kTestByColor',set/color)[1])]"/>
  </compound>
 </xsl:template>

 <xsl:template match="test">
  <set>
   <color><xsl:value-of select="set/color"/></color>
   <xsl:copy-of select="key('kTestByColor',set/color)/title"/>
  </set>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<compound>
    <test>
        <title>k</title>
        <cetval>
            <cat>abc</cat>
            <cat>fcg</cat>
        </cetval>
        <value>1</value>
        <set>
            <color>blue</color>
        </set>
    </test>
    <test>
        <title>p</title>
        <cetval>
            <cat>fcg</cat>
            <cat>klm</cat>
        </cetval>
        <value>10</value>
        <set>
            <color>pink</color>
        </set>
    </test>
    <test>
        <title>j</title>
        <cetval>
            <cat>fcg</cat>
            <cat>klm</cat>
            <cat>abc</cat>
        </cetval>
        <value>484</value>
        <set>
            <color>yellow</color>
        </set>
    </test>
    <test>
        <title>v</title>
        <cetval>
            <cat>dji</cat>
            <cat>kfjsdlk</cat>
        </cetval>
        <value>93</value>
    </test>
    <test>
        <title>r</title>
        <cetval>
            <cat>deiu</cat>
            <cat>kfjdf</cat>
            <cat>abc</cat>
        </cetval>
        <value>10</value>
        <set>
            <color>blue</color>
        </set>
        <test>
            <title>i</title>
            <cetval>
                <cat>fcg</cat>
                <cat>klm</cat>
                <cat>abc</cat>
            </cetval>
            <value>10</value>
        </test>
        <test>
            <title>w</title>
            <cetval>
                <cat>diweif</cat>
                <cat>fjf</cat>
                <cat>abc</cat>
            </cetval>
            <value>10</value>
            <set>
                <color>yellow</color>
            </set>
        </test>
    </test>
</compound>

产生了想要的正确结果:

<compound>
   <cat>abc</cat>
   <set>
      <color>blue</color>
      <title>k</title>
      <title>r</title>
   </set>
   <set>
      <color>yellow</color>
      <title>j</title>
      <title>w</title>
   </set>
</compound>

如果你想为所有cat的disting值产生类似的结果,那么它又简短又简单

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

 <xsl:key name="kCatByVal" match="cat" use="."/>
 <xsl:key name="kColorByVal" match="color" use="."/>
 <xsl:key name="kTestByCat" match="test" use="cetval/cat"/>

 <xsl:variable name="vDistinctColors" select=
  "/*/*/set/color[generate-id()=generate-id(key('kColorByVal',.)[1])]"/>

 <xsl:template match="/">
  <xsl:apply-templates select=
   "/*/*/cetval/cat
        [generate-id()
        = generate-id(key('kCatByVal',.)[1])]"/>
 </xsl:template>

 <xsl:template match="cat">
  <xsl:variable name="vcurCat" select="."/>
  <compound>
    <cat><xsl:apply-templates/></cat>
    <xsl:for-each select="$vDistinctColors">
     <xsl:apply-templates select=
      "(key('kCatByVal',$vcurCat)/../../set/color[.=current()])[1]">
       <xsl:with-param name="pCat" select="$vcurCat"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </compound>
 </xsl:template>

 <xsl:template match="color">
  <xsl:param name="pCat"/>

  <xsl:copy-of select="."/>
  <xsl:copy-of select="key('kTestByCat',$pCat)[set/color=current()]/title"/>
 </xsl:template>
</xsl:stylesheet>

当应用于同一XML文档(上图)时,结果现在为:

<compound>
   <cat>abc</cat>
   <color>blue</color>
   <title>k</title>
   <title>r</title>
   <color>yellow</color>
   <title>j</title>
   <title>w</title>
</compound>
<compound>
   <cat>fcg</cat>
   <color>blue</color>
   <title>k</title>
   <color>pink</color>
   <title>p</title>
   <color>yellow</color>
   <title>j</title>
</compound>
<compound>
   <cat>klm</cat>
   <color>pink</color>
   <title>p</title>
   <color>yellow</color>
   <title>j</title>
</compound>
<compound>
   <cat>dji</cat>
</compound>
<compound>
   <cat>kfjsdlk</cat>
</compound>
<compound>
   <cat>deiu</cat>
   <color>blue</color>
   <title>r</title>
</compound>
<compound>
   <cat>kfjdf</cat>
   <color>blue</color>
   <title>r</title>
</compound>