Applying Muenchian grouping for a simple XML with XSLT

。_饼干妹妹 提交于 2019-11-30 15:19:57

问题


How does Muenchian grouping work in details?

I have a simple XML document generated from a database:

<CLIENTS>
    <CLIENT>
       <NAME>John</NAME>
       <ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
       <LAST_USED>2012-10-03</LAST_USED>
       <AMOUNT>5000</AMOUNT>

    </CLIENT>
    <CLIENT>
       <NAME>John</NAME>
       <ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
       <LAST_USED>2012-10-02</LAST_USED>
       <AMOUNT>10000</AMOUNT>
    </CLIENT>
       ...

I'd like to group by the name node. How can I the desired ouput to be the following?

<ClIENTS>
    <CLIENT>
        <NAME>John</NAME>
        <ACCOUNT>
           <ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
           <LAST_USED>2012-10-03</LAST_USED>
           <AMOUNT>5000</AMOUNT>
        </ACCOUNT>
        <ACCOUNT>
           <ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
           <LAST_USED>2012-10-03</LAST_USED>
           <AMOUNT>10000</AMOUNT>
        </ACCOUNT>
       ....
</CLIENTS>

回答1:


Read www.jenitennison.com/xslt/grouping/muenchian.xml, for a help with the code define a key

<xsl:key name="client-by-name" match="CLIENT" use="NAME"/>

then use templates as

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


<xsl:template match="CLIENTS">
  <xsl:copy>
    <xsl:apply-templates select="CLIENT[generate-id() = generate-id(key('client-by-name', NAME)[1])]" mode="group"/>
  <xsl:copy>
</xsl:template>

<xsl:template match="CLIENT" mode="group">
  <xsl:copy>
    <xsl:copy-of select="NAME"/>
    <xsl:apply-templates select="key('client-by-name', NAME)"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="CLIENT">
  <ACCOUNT>
    <xsl:apply-templates select="node()[not(self::NAME)]"/>
  </ACCOUNT>
</xsl:template>

[edit] If you want to use XSLT 2.0 then of course you don't need Muenchian grouping, instead you use

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

<xsl:template match="CLIENTS">
  <xsl:copy>
    <xsl:for-each-group select="CLIENT" group-by="NAME">
      <CLIENT>
        <xsl:apply-templates select="NAME, current-group()"/>
      </CLIENT>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="CLIENT">
  <ACCOUNT>
    <xsl:apply-templates select="node() except NAME"/>
  </ACCOUNT>
</xsl:template>



回答2:


Jeni Tennison breaks the steps required to perform Muenchian grouping here:

http://www.jenitennison.com/xslt/grouping/muenchian.html

Essentially, use XSLT to assign a key to the node, this key can be repeated if there's identical nodes in the document. The XSLT then goes through each key, and allows you to output the nodes with matching keys.

So, in Martin's answer, this line is creating a key for each CLIENT based on the content of the NAME node (remember if the NAME is the same for multiple clients, so will the key):

<xsl:key name="client-by-name" match="CLIENT" use="NAME"/>

You then want to go through all the keys and find the first instance of each (again using Martin's answer)

<xsl:apply-templates select="CLIENT[generate-id() = generate-id(key('client-by-name', NAME)[1])]" mode="group"/>

You then want to find all of the CLIENTS that match the key to be able to output their detail (again, Martins)

<xsl:apply-templates select="key('client-by-name', NAME)"/>

From here you'd need another template to output the CLIENT details




回答3:


Muenchian Grouping (as per @Martin's answer) eliminates the redundancy that a more traditional grouping strategy has whilst grouping.

Without Muenchian Grouping, templates usually used preceding-sibling or following-sibling to determine the first candidate instance of each group, and then would require a second query to lookup all nodes matching the group, as follows:

<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:template match="CLIENTS">
        <CLIENTS>
            <!--Only find the 'first' instance of each client-->
            <xsl:apply-templates select="CLIENT[not(NAME = preceding-sibling::CLIENT/NAME)]" mode="client"/>
        </CLIENTS>
    </xsl:template>

    <xsl:template match="CLIENT" mode="client">
        <xsl:variable name="name" select="NAME"/>
        <CLIENT>
            <NAME>
                <xsl:value-of select="$name"/>
            </NAME>
            <ACCOUNTS>
                <!--Note that we now have to find the other matching clients *again* - this is the inefficiency that Muenchian grouping eliminates-->
                <xsl:apply-templates select="/CLIENTS/CLIENT[NAME/text()=$name]" mode="account" />
            </ACCOUNTS>
        </CLIENT>
    </xsl:template>

    <xsl:template match="CLIENT" mode="account">
        <ACCOUNT>
            <!--Copy everything else except Name, which is the grouping key -->
            <xsl:copy-of select="@* | *[not(local-name='NAME')]"/>
        </ACCOUNT>
    </xsl:template>

</xsl:stylesheet>



回答4:


In a previous comment (under @Martin's answer), the OP asked if XSLT 2.0's for-each-group element could be used to solve this problem.

When this XSLT 2.0 solution:

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

  <xsl:template match="/*">
    <CLIENTS>
      <xsl:for-each-group select="CLIENT" group-by="NAME">
        <CLIENT>
          <xsl:sequence select="NAME" />
          <xsl:for-each select="current-group()">
            <ACCOUNT>
              <xsl:sequence select="*[not(self::NAME)]" />
            </ACCOUNT>
          </xsl:for-each>
        </CLIENT>
      </xsl:for-each-group>
    </CLIENTS>
  </xsl:template>

</xsl:stylesheet>

...is applied to the OP's original XML:

<CLIENTS>
  <CLIENT>
    <NAME>John</NAME>
    <ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
    <LAST_USED>2012-10-03</LAST_USED>
    <AMOUNT>5000</AMOUNT>
  </CLIENT>
  <CLIENT>
    <NAME>John</NAME>
    <ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
    <LAST_USED>2012-10-02</LAST_USED>
    <AMOUNT>10000</AMOUNT>
  </CLIENT>
</CLIENTS>

...the wanted result is produced:

<?xml version="1.0" encoding="utf-8"?>
<CLIENTS>
  <CLIENT>
    <NAME>John</NAME>
    <ACCOUNT>
      <ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
      <LAST_USED>2012-10-03</LAST_USED>
      <AMOUNT>5000</AMOUNT>
    </ACCOUNT>
    <ACCOUNT>
      <ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
      <LAST_USED>2012-10-02</LAST_USED>
      <AMOUNT>10000</AMOUNT>
    </ACCOUNT>
  </CLIENT>
</CLIENTS>

Explanation:

As you have already correctly surmised, XSLT 2.0 introduced the for-each-group element (and its associated partners, such as current-group()) in order to do away with amazing/impressive, yet potentially confusing grouping methodologies like the Muenchian Method.



来源:https://stackoverflow.com/questions/12783340/applying-muenchian-grouping-for-a-simple-xml-with-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!