XSLT函数——key()

       key(string,object)函数用于在一个索引中依照key值搜索对应的节点。参数string用于指定索引对象的名字,参数object用于指定查找时的key值。

       语法:key(string,object)

       参数:string字符串,必需;object字符串,必需

      返回值:节点集合

示例:

xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <customers>
  3.     <customer>
  4.         <name>Alex</name>
  5.         <age>25</age>
  6.         <country>China</country>
  7.     </customer>
  8.     <customer>
  9.         <name>Kin</name>
  10.         <age>27</age>
  11.         <country>England</country>
  12.     </customer>
  13.     <customer>
  14.         <name>Kevin</name>
  15.         <age>28</age>
  16.         <country>China</country>
  17.     </customer>
  18.     <customer>
  19.         <name>Bob</name>
  20.         <age>25</age>
  21.         <country>England</country>
  22.     </customer>
  23.     <customer>
            <name>John</name>
            <age>25</age>
            <country>America</country>
        </customer>
        <customer>
            <name>Joe</name>
            <age>32</age>
            <country>England</country>
        </customer>
        <customer>
            <name>Mark</name>
            <age>25</age>
            <country>America</country>
        </customer>
    </customers>

xslt:

  1. <?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="gb2312" indent="yes"/>    
        <xsl:key match="customer" name="customerKey" use="country"/>
        <xsl:template match="customers">
            <xsl:copy>
                <xsl:for-each select="customer[generate-id(.)=generate-id(key('customerKey',country))]">
                    <xsl:sort select="country"/>                
                    <xsl:for-each select="../customer[country=current()/country]">
                        <xsl:sort select="name"/>
                        <xsl:copy-of select="."/>                
                    </xsl:for-each>   
                </xsl:for-each>                
            </xsl:copy>    
        </xsl:template>
    </xsl:stylesheet>

结果:

  1. <?xml version="1.0" encoding="gb2312"?>
    <customers>
        <customer>
            <name>John</name>
            <age>25</age>
            <country>America</country>
        </customer>
        <customer>
            <name>Mark</name>
            <age>25</age>
            <country>America</country>
        </customer>
        <customer>
            <name>Alex</name>
            <age>25</age>
            <country>China</country>
        </customer>
        <customer>
            <name>Kevin</name>
            <age>28</age>
            <country>China</country>
        </customer>
        <customer>
            <name>Bob</name>
            <age>25</age>
            <country>England</country>
        </customer>
        <customer>
            <name>Joe</name>
            <age>32</age>
            <country>England</country>
    </customer>
        <customer>
            <name>Kin</name>
            <age>27</age>
            <country>England</country>
        </customer>
    </customers>

转载于:https://www.cnblogs.com/zhaozhan/archive/2010/01/17/1649855.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值