开发者

Substring and Grouping issue in XSLT 2.0

I have the following XML:

<InterSection criteria="Microsoft-ASP">value</Intersection>
<InterSection criteria="Microsoft-MVC">value</Intersection>
<InterSection criteria="HP-MVC">value</Intersection>

I need to create TOC at top of my HTML page and so I need to group by criteria (First part before '-').

I need distinct values like

  1. Microsoft
  2. HP

My XSLT have following code

<a>
    <xsl:attribute name="href">
      <xsl:text>#trigger</xsl:text>
      <xsl:value-of select="position()"/>
    </xsl:attribute>
                <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
                      <xsl:value-of select="substring-before(@Criteria, ' - ')"/> 
                      <xsl:t开发者_StackOverflow社区ext>
                      </xsl:text>
                    </xsl:for-each-group>        

  </a>

But somehow i am getting

  1. Microsoft
  2. Microsoft
  3. HP

instead of 1. Microsoft 2. HP

I have following written in my XSLT

  <xsl:template match="InterSection" mode="TOC">

<li>
  <a>
    <xsl:attribute name="href">
      <xsl:text>#trigger</xsl:text>
      <xsl:value-of select="position()"/>
    </xsl:attribute>

    <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
        <xsl:value-of select="current-grouping-key()"/>
        <xsl:for-each select="current-group()"/>
     </xsl:for-each-group>
  </a>
</li>


I'm finally going to summarize the problems I would fix in your work in order to make it work (deleted previous edits):

  • your input xml elements are not in correct case
  • in your transform you refer to the attribute criteriausing the wrong case
  • your substring expression contains too much spaces around the second argument -
  • you should use current-grouping-key() to get the value of the current matching group
  • the template must match the parent of InterSection (Root) and then you have to iterate on InterSection like you would do with xsl:for-each


           <xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">

This is meaningless. The expression in the select attribute selects a single item. Grouping has meaning when there are more than one items selected and we want to group them using certain criteria.

When a single item is "grouped" the "result" is always that same item -- this is exactly what you get.

Solution: Select correctly a group of items to be grouped. Or, use this alternative solution:

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

 <xsl:variable name="vallKeys" select=
  "distinct-values
      (/*/*/@criteria
            /substring-before(.,'-')
      )
  "/>
 <xsl:template match="/">
  <xsl:for-each select="$vallKeys">
   <xsl:value-of select=
     "concat(position(),'.',.,'&#xA;')"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document (derived from the provided fragment by corrected its severe malformedness):

<someParent>
    <InterSection criteria="Microsoft-ASP">value</InterSection>
    <InterSection criteria="Microsoft-MVC">value</InterSection>
    <InterSection criteria="HP-MVC">value</InterSection>
</someParent>

the wanted, correct result is produced:

1.Microsoft
2.HP
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜