English 中文(简体)
Performing a "Group By" query in XPath XSL
原标题:

Given the following XML:

<results name="queryResults">
  <int name="intfield1:[* TO 10]">11</int> 
  <int name="intfield2:[10 TO 20]">9</int> 
  <int name="intfield1:[10 TO 20]">12</int> 
</results>

I would like to produce this XML:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
        <value name="[10 TO 10]">12</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
</results>

I can t think how to do this in XSL mainly because i m wanting to Group by the numbericfield.. All i can come up with is this:

<xsl:if test="count(results/int) &gt; 0">
    <results>
    <xsl:for-each select="results/int">
        <field>
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(@name,  : )"/></xsl:attribute>
            <value>
                <xsl:attribute name="name">
                    <xsl:value-of select="substring-after(@name,  : ) "/>
                </xsl:attribute>
                <xsl:value-of select="."/>
            </value>
        </field>
    </xsl:for-each>
    </results>
</xsl:if>

However this doesn t produce the nice grouped list instead i get this:

<results>
    <field name="numberfield1">
        <value name="[* TO 10]">11</value>
    </field>
    <field name="numberfield2">
        <value name="[10 TO 20]">9</value>
    </field>
    <field name="numberfield1">
        <value name="[10 TO 10]">12</value>
    </field>
</results>

If someone can stear me in the right direction.. That would be great?

Thanks

最佳回答

To do this in XSLT 1.0, you will have to use a technique called "muenchian grouping". First create a key of the nodes on which you wish to group

<xsl:key name="intfield" match="int" use="substring-before(@name,  : )" />

Next, you iterate it through all the nodes, but only selecting the ones that happen to be first in the relevant group

<xsl:for-each select="int[generate-id() = generate-id(key( intfield , substring-before(@name,  : ))[1])]">

Next, you can iterate use the key to iterate over all nodes in the group

<xsl:variable name="intfieldname" select="substring-before(@name,  : )"/>
<xsl:for-each select="key( intfield , $intfieldname)">

Putting this all together gives

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml"/>
   <xsl:key name="intfield" match="int" use="substring-before(@name,  : )"/>
   <xsl:template match="/results">
      <results>
         <xsl:for-each select="int[generate-id() = generate-id(key( intfield , substring-before(@name,  : ))[1])]">
            <xsl:variable name="intfieldname" select="substring-before(@name,  : )"/>
            <field>
               <xsl:attribute name="name">
                  <xsl:value-of select="$intfieldname"/>
               </xsl:attribute>
               <xsl:for-each select="key( intfield , $intfieldname)">
                  <value>
                     <xsl:attribute name="name">
                        <xsl:value-of select="substring-after(@name,  : )"/>
                     </xsl:attribute>
                     <xsl:value-of select="."/>
                  </value>
               </xsl:for-each>
            </field>
         </xsl:for-each>
      </results>
   </xsl:template>
</xsl:stylesheet>

In your example, intfield becomes numberfield though. I have kept the name as intfield in the above example.

  • fixed typo.
问题回答

Muenchian grouping is a work of genius. It s not easy to understand, but see: http://www.jenitennison.com/xslt/grouping/muenchian.html

To simplify the process the W3C specifically supported grouping in XSLT2.0. See, for example: http://www.xml.com/pub/a/2003/11/05/tr.html

However not all environments support XSLT2.0





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签