分组排序和

时间:2019-06-01 20:28:21

标签: xml xslt

有。 这是XML和一些规则:  1.按楼层/房间分组的项目  2.按编号/代码对项目排序  3.总和/数量,其中描述不包含“配置文件”  4.“ PROFILE”的描述必须位于每个组的顶部

这是我的xslt:

{<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="kFloorAndRoom" match="Item" use="concat(@Floor, '|', @Room)"/>
<xsl:key name="kRefAndCode" match="Item" use="concat(@Floor, '|', @Room, '|', @Ref, '|', @Code)"/>
<xsl:template match="Purchase">
    <Purchase xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Items>
            <xsl:for-each select="Items/Item[generate-id(.)=generate-id(key('kFloorAndRoom', concat(@Floor, '|', @Room))[1])]">
                <xsl:sort select="@Ref"/>
                <xsl:sort select="@Code"/>
                <xsl:call-template name="FloorRoom">
                    <xsl:with-param name="FloorRoomItem" select="key('kFloorAndRoom', concat(@Floor, '|', @Room))"/>
                </xsl:call-template>
            </xsl:for-each>
        </Items>
    </Purchase>
</xsl:template>
<xsl:template name="FloorRoom">
    <xsl:param name="FloorRoomItem"/>
    <xsl:for-each select="$FloorRoomItem">
        <xsl:sort select="@Description"/>
        <xsl:if test="(contains(@Description,'PROFILE'))">
            <xsl:element name="Item">
                <xsl:copy-of select="@Floor"/>
                <xsl:copy-of select="@Room"/>
                <xsl:copy-of select="@Description"/>
                <xsl:copy-of select="@Ref"/>
                <xsl:copy-of select="@Code"/>
                <xsl:copy-of select="@Quantity"/>
            </xsl:element>
        </xsl:if>
    </xsl:for-each>
    <xsl:call-template name="RefCode">
        <xsl:with-param name="RefCodeItem" select="$FloorRoomItem[not(contains(@Description, 'PROFILE'))]"/>
    </xsl:call-template>
</xsl:template>
<xsl:template name="RefCode">
    <xsl:param name="RefCodeItem"/>
    <xsl:for-each select="$RefCodeItem[generate-id(.)=generate-id(key('kRefAndCode', concat(@Floor, '|', @Room, '|', @Ref, '|', @Code))[1])]">
        <xsl:sort select="@Description"/>
        <xsl:element name="Item">
            <xsl:copy-of select="@Floor"/>
            <xsl:copy-of select="@Room"/>
            <xsl:copy-of select="@Description"/>
            <xsl:copy-of select="@Ref"/>
            <xsl:copy-of select="@Code"/>
            <xsl:variable name="Nodes" select="key('kRefAndCode', concat(@Floor, '|', @Room, '|', @Ref, '|', @Code))"/>
            <xsl:attribute name="Quantity">
                <xsl:value-of select="sum($Nodes/@Quantity)"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:for-each>
</xsl:template>


}

这是我运行xslt之前的xml:

<Purchase Project="JB House" Customer="aa" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<Items>
		<Item Ref="P01" Code="399 01 00 W" Quantity="1.421" Description="MIC40/40+ - PROFILE W" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 01 02 05 SBL HE" Quantity="1.421" Description="MIC40/60 - PC SBL PROFILE HE" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 01 03 01 W" Quantity="1" Description="MIC40 - END CAP W" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 610 30 E" Quantity="1" Description="MIC40/60 - LED ARRAY 1 x 30W DOWN" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 01 00 W" Quantity="1.421" Description="MIC40/40+ - PROFILE W" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 01 02 05 SBL HE" Quantity="1.421" Description="MIC40/60 - PC SBL PROFILE HE" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 01 03 01 W" Quantity="1" Description="MIC40 - END CAP W" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 06 06" Quantity="1" Description="MIC40/60 - CORNER 90" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="P01" Code="399 610 30 E" Quantity="1" Description="MIC40/60 - LED ARRAY 1 x 30W DOWN" Floor="Gelijkvloers" Room="01_Bureau" Group="P01"/>
		<Item Ref="L05" Code="202 272 811 942 W" Quantity="1" Description="DEEP RINGO LED 94033 W" Floor="04 - Marktplaats -1" Room="-1.08 - Gang" Group=""/>
	</Items>
</Purchase>

这是我运行xslt之后的xml:

<?xml version="1.0" encoding="utf-8"?>
<Purchase xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Items>
      <Item Floor="04 - Marktplaats -1" Room="-1.08 - Gang" Description="DEEP RINGO LED 94033 W" Ref="L05" Code="202 272 811 942 W" Quantity="1"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/40+ - PROFILE W" Ref="P01" Code="399 01 00 W" Quantity="1.421"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/40+ - PROFILE W" Ref="P01" Code="399 01 00 W" Quantity="1.421"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/60 - PC SBL PROFILE HE" Ref="P01" Code="399 01 02 05 SBL HE" Quantity="1.421"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/60 - PC SBL PROFILE HE" Ref="P01" Code="399 01 02 05 SBL HE" Quantity="1.421"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40 - END CAP W" Ref="P01" Code="399 01 03 01 W" Quantity="2"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/60 - CORNER 90" Ref="P01" Code="399 06 06" Quantity="1"/>
      <Item Floor="Gelijkvloers" Room="01_Bureau" Description="MIC40/60 - LED ARRAY 1 x 30W DOWN" Ref="P01" Code="399 610 30 E" Quantity="2"/>
   </Items>
</Purchase>

我的XSLT似乎正常工作。我想知道是否有更简单的XSLT。

0 个答案:

没有答案