XSLT1.0 中两个联系人的分组和合并 [英] Grouping and merging of two contacts in XSLT1.0

查看:20
本文介绍了XSLT1.0 中两个联系人的分组和合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 xml 文件:

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</LastName>
<Email>nuraaa_iceee@gmail.com</Email>

</CONTACT>
</CONTACTS>

1.如何将以上2个联系人合并为同一人的单个联系人

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

我想要这样的输出:

<?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>

我不确定我是否可以用 current-group() 和 current-grouping-key() 做到这一点.非常感谢您的支持.

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

user639175,他帮助解决了这个问题,他的解决方案有效,但没有给出我想要的输出.所以我以一种简单的方式改变了这个问题.

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.

推荐答案

<?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>

它应该可以工作:)

这篇关于XSLT1.0 中两个联系人的分组和合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆