Xml 按XSLT 1.0分组,分组
我有点需要使用XSLT创建“组组” 下面是我的XML和XSL文件。我用它来应用明钦族的分组。我试图缩短文件,只显示表示问题的必需元素 XML文件Xml 按XSLT 1.0分组,分组,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有点需要使用XSLT创建“组组” 下面是我的XML和XSL文件。我用它来应用明钦族的分组。我试图缩短文件,只显示表示问题的必需元素 XML文件 <IO_SearchShoppingFilesResult xmlns="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <ShoppingFiles
<IO_SearchShoppingFilesResult xmlns="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ShoppingFiles
xmlns:a="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping"
xmlns:b="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Air">
<a:T_ShoppingFile>
....
<b:T_AirBookingItem>
...
<a:LocalPaxType>ADT</a:LocalPaxType>
...
</b:T_AirBookingItem>
<b:T_AirBookingItem>
...
<a:LocalPaxType>CHD</a:LocalPaxType>
...
</b:T_AirBookingItem>
<b:T_AirBookingItem>
...
<a:LocalPaxType>INF</a:LocalPaxType>
...
</b:T_AirBookingItem>
...
</a:T_ShoppingFile>
<a:T_ShoppingFile>
....
<b:T_AirBookingItem>
...
<a:LocalPaxType>ADT</a:LocalPaxType>
...
</b:T_AirBookingItem>
...
</a:T_ShoppingFile>
</ShoppingFiles>
</IO_SearchShoppingFilesResult>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:res="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping"
xmlns:a="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping"
xmlns:b="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Air"
xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:bb="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping.Views">
<xsl:output method="xml" indent="yes" />
<xsl:key name="travelerGroup"
match="res:IO_SearchShoppingFilesResult/res:ShoppingFiles/a:T_ShoppingFile[position()=1]/a:AirBookings/b:T_AirBooking/b:BookingItems/b:T_AirBookingItem"
use="b:PaxReference/a:LocalPaxType" />
<xsl:template match="/">
<xsl:element name="PNRViewRS">
<xsl:apply-templates
select="res:IO_SearchShoppingFilesResult/res:ShoppingFiles" />
</xsl:element>
</xsl:template>
<xsl:template match="res:ShoppingFiles">
<!-- For FareGroup, Traveler, Telephone, EmailAddress -->
<xsl:apply-templates select="a:T_ShoppingFile" />
...
</xsl:template>
<xsl:template match="a:T_ShoppingFile">
...
<xsl:apply-templates
select="a:AirBookings/b:T_AirBooking/b:BookingItems/b:T_AirBookingItem[generate-id() = generate-id(key('travelerGroup', b:PaxReference/a:LocalPaxType)[1])]" />
...
</xsl:template>
...
<xsl:template match="b:T_AirBookingItem">
...
<!-- Line 1 -->
<xsl:value-of
select="count(key('travelerGroup', b:PaxReference/a:LocalPaxType))" />
...
</xsl:template>
....
...
ADT
...
...
冠心病
...
...
INF
...
...
....
...
ADT
...
...
XSL文件
<IO_SearchShoppingFilesResult xmlns="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ShoppingFiles
xmlns:a="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping"
xmlns:b="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Air">
<a:T_ShoppingFile>
....
<b:T_AirBookingItem>
...
<a:LocalPaxType>ADT</a:LocalPaxType>
...
</b:T_AirBookingItem>
<b:T_AirBookingItem>
...
<a:LocalPaxType>CHD</a:LocalPaxType>
...
</b:T_AirBookingItem>
<b:T_AirBookingItem>
...
<a:LocalPaxType>INF</a:LocalPaxType>
...
</b:T_AirBookingItem>
...
</a:T_ShoppingFile>
<a:T_ShoppingFile>
....
<b:T_AirBookingItem>
...
<a:LocalPaxType>ADT</a:LocalPaxType>
...
</b:T_AirBookingItem>
...
</a:T_ShoppingFile>
</ShoppingFiles>
</IO_SearchShoppingFilesResult>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:res="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping"
xmlns:a="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping"
xmlns:b="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Air"
xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:bb="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping.Views">
<xsl:output method="xml" indent="yes" />
<xsl:key name="travelerGroup"
match="res:IO_SearchShoppingFilesResult/res:ShoppingFiles/a:T_ShoppingFile[position()=1]/a:AirBookings/b:T_AirBooking/b:BookingItems/b:T_AirBookingItem"
use="b:PaxReference/a:LocalPaxType" />
<xsl:template match="/">
<xsl:element name="PNRViewRS">
<xsl:apply-templates
select="res:IO_SearchShoppingFilesResult/res:ShoppingFiles" />
</xsl:element>
</xsl:template>
<xsl:template match="res:ShoppingFiles">
<!-- For FareGroup, Traveler, Telephone, EmailAddress -->
<xsl:apply-templates select="a:T_ShoppingFile" />
...
</xsl:template>
<xsl:template match="a:T_ShoppingFile">
...
<xsl:apply-templates
select="a:AirBookings/b:T_AirBooking/b:BookingItems/b:T_AirBookingItem[generate-id() = generate-id(key('travelerGroup', b:PaxReference/a:LocalPaxType)[1])]" />
...
</xsl:template>
...
<xsl:template match="b:T_AirBookingItem">
...
<!-- Line 1 -->
<xsl:value-of
select="count(key('travelerGroup', b:PaxReference/a:LocalPaxType))" />
...
</xsl:template>
...
...
...
...
...
...
大家可以看到,我已经在
上应用了键。多个
中存在多个
现在,我想分别处理每个
,然后对其中存在的所有
应用分组。在此代码中发生的是,所有
中的所有
一次都分组在一起
第1行显示了此转换的一个结果。对于单个
,它应该显示特定
(比如ADT)的
总数。但是,它考虑了整个XML中的所有
所以我应该得到“1”个ADT,我得到的是“2”
如何处理它?听起来好像您想使用一个由多个元素组成的复合键。在本例中,您是通过a:T\u ShoppingFile和a:LocalPaxType进行挖掘的,因此您可能需要这样的内容
<xsl:key
name="item"
match="b:T_AirBookingItem"
use="concat(generate-id(..) , '|', a:LocalPaxType)" />
然后,对于每个a:T\u ShoppingFile要获得唯一的a:LocalPaxType记录,只需执行以下操作
<xsl:apply-templates
select="b:T_AirBookingItem[generate-id()
= generate-id(key('item', concat(generate-id(..) , '|', a:LocalPaxType))[1])]" />
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:res="http://schemas.datacontract.org/2004/07/Trevoo.WS.IO.Shopping" xmlns:a="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping" xmlns:b="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Air" xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:bb="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Shopping.Views" exclude-result-prefixes="res a b c bb">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="item" match="b:T_AirBookingItem" use="concat(generate-id(..) , '|', a:LocalPaxType)"/>
<xsl:template match="/">
<xsl:apply-templates select="//a:T_ShoppingFile"/>
</xsl:template>
<xsl:template match="a:T_ShoppingFile">
<file number="{position()}">
<xsl:apply-templates select="b:T_AirBookingItem[generate-id() = generate-id(key('item', concat(generate-id(..) , '|', a:LocalPaxType))[1])]"/>
</file>
</xsl:template>
<xsl:template match="b:T_AirBookingItem">
<result>
<xsl:value-of select="concat(a:LocalPaxType, ' * ', count(key('item', concat(generate-id(..) , '|', a:LocalPaxType))), ' ')"/>
</result>
</xsl:template>
</xsl:stylesheet>
应用于XML时,将输出以下内容
<file number="1">
<result>ADT * 1</result>
<result>CHD * 1</result>
<result>INF * 1</result>
</file>
<file number="2">
<result>ADT * 1</result>
</file>
ADT*1
冠心病*1
INF*1
ADT*1
显然,这显示了所有a:T_ShoppingFile和a:LocalPaxType的结果。如果您想限制为一个特定的a:LocalPaxType,您可以这样做(尽管您可能希望参数化该值,而不是硬编码ADT
<xsl:template match="a:T_ShoppingFile">
<file number="{position()}">
<xsl:value-of select="count(key('item', concat(generate-id() , '|', 'ADT')))" />
</file>
</xsl:template>
这真的很有帮助。精确地解决了我的问题。谢谢:)使用“复合键”的想法很棒!!确保键的两个部分中都不会出现连接这两个部分的分隔符(在本例中为条)。