开发者

Grouping and merging of two contacts in XSLT1.0

This is my xml file:

<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>

<CONTACT>
<FirstName>Arun</FirstName>
<LastName>Arun_Neelam</LastName>
<Email>nuraaa_iceee@yahoo.co.in</Email>
</CONTACT>

<CONTACT>
<FirstName>Arun</FirstName>
<LastName>Arun_Neelam</Last开发者_JS百科Name>
<Email>nuraaa_iceee@gmail.com</Email>

</CONTACT>
</CONTACTS>

1.How can i merge the above 2 contacts as single contact which is belongs to the same person

I would like to have an output like this:

<?xml version="1.0" encoding="windows-1250"?>
<CONTACTS>
    <CONTACT>
        <FirstName>Arun</FirstName>
        <LastName>Arun_Neelam</LastName>
        <Email>nuraaa_iceee@gmail.com</Email>
        <Email>nuraaa_iceee@yahoo.co.in</Email>
</CONTACT>
</CONTACTS>

I'm not sure I can do it with current-group() and current-grouping-key(). Thank you very much for your support.

user639175, he helped for this problem and his solution is working but not giving my desired output. So i have changed the question in a simple way.

Note: I've formatted the question once again completely to avoid confusion in the same thread.


Using a standard grouping technique, you need just two templates (no loops, no params):

  • Use a composite key to collect CONTACT by FirstName and SecondName.
  • Then apply the templates to one single CONTACT for each group.
  • Finally, retrieve the Email nodes from the key.

Your final transform:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="k_Contacts" 
        match="CONTACTS/CONTACT" 
        use="concat(FirstName,LastName)"/>

    <xsl:template match="CONTACTS">
        <xsl:copy>
            <xsl:apply-templates select="CONTACT[generate-id()=
                generate-id(key('k_Contacts',concat(FirstName,LastName))[1])]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>
            <xsl:copy-of select="FirstName | LastName"/>
            <xsl:copy-of select="key('k_Contacts',concat(FirstName,LastName))
              /Email"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

In XSLT 2.0 you can use xsl:for-each-group; just replace

<xsl:apply-templates select="CONTACT[generate-id()=
                generate-id(key('k_Contacts',concat(FirstName,LastName))[1])]"/>

with:

    <xsl:for-each-group select="CONTACT" group-by="concat(FirstName,LastName)">
        <xsl:apply-templates select="current-group()[1]"/>
    </xsl:for-each-group>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"  encoding="windows-1250" indent="yes" />

<xsl:key name="groupName" match="//CONTACTS/CONTACT" use="concat(FirstName, LastName)" />

<xsl:template match="CONTACTS">

  <CONTACTS>
  <xsl:for-each select="//CONTACTS/CONTACT[generate-id() = generate-id( key('groupName', concat(FirstName, LastName))   [1] ) ]" >
    <xsl:sort select="CONTACT/EMail"  />

      <xsl:call-template name="group">
        <xsl:with-param name="k1" select="FirstName" />
        <xsl:with-param name="k2" select="LastName" />
      </xsl:call-template>

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

<xsl:template name="group">
<xsl:param name="k1" /> 
<xsl:param name="k2" /> 

  <CONTACT>
  <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][1]">

    <xsl:copy-of select="FirstName" />       
    <xsl:copy-of select="LastName" />       
    <!-- here we have the first Email -->
    <xsl:copy-of select="EMail" />       

  </xsl:for-each>
  <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][position() &gt; 1]">
    <!-- here we have the next Email -->

    <xsl:copy-of select="EMail" />       

  </xsl:for-each>
  </CONTACT>

</xsl:template> 
</xsl:stylesheet>

It should work :)


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"  encoding="windows-1250" indent="yes" />

<xsl:key name="groupName" match="//CONTACTS/CONTACT" use="concat(FirstName, LastName)" />

<xsl:template match="CONTACTS">

  <CONTACTS>
  <xsl:for-each select="//CONTACTS/CONTACT[generate-id() = generate-id( key('groupName', concat(FirstName, LastName))   [1] ) ]" >
    <xsl:sort select="CONTACT/EMail"  />

      <xsl:call-template name="group">
        <xsl:with-param name="k1" select="FirstName" />
        <xsl:with-param name="k2" select="LastName" />
      </xsl:call-template>

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

<xsl:template name="group">
<xsl:param name="k1" /> 
<xsl:param name="k2" /> 


  <xsl:for-each select="//CONTACTS/CONTACT[FirstName = $k1][LastName = $k2][1]">

    <xsl:copy-of select="." />       

  </xsl:for-each>

</xsl:template> 
</xsl:stylesheet>

I did not exclude Arun - because your exmaple has a flaw ? otherwise it works :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜