在XSLT 1.0中使用muenchian基于字段值对元素进行分组

时间:2014-04-21 07:21:22

标签: xslt-1.0

请求xml ..

<tem:responseDA xmlns:tem="http://tempuri.org">
<tem:outputData>   
<tem:dictionary id="AutoOutputs">    

<tem:list numOfItems="2">       
<tem:item>       
<tem:field dataType="double" name="DOWNPAYMENT">2000.00</tem:field>
</tem:item>     
<tem:item>       
<tem:field dataType="double" name="DOWNPAYMENT">3000.00</tem:field>
</tem:item>
</tem:list>  
<tem:list numOfItems="2">       
<tem:item>       
<tem:field dataType="string" name="CAMPAIGNCODE">A</tem:field>\
</tem:item>
<tem:item>       
<tem:field dataType="string" name="CAMPAIGNCODE">B</tem:field>
</tem:item>
</tem:list>   
<tem:list numOfItems="2">       
<tem:item>      
<tem:field dataType="double" name="BALLOONPAYMENT">4000.00</tem:field>
</tem:item>
<tem:item>       
<tem:field dataType="double" name="BALLOONPAYMENT">5000.00</tem:field>
</tem:item>
</tem:list>       
</tem:dictionary>
</tem:outputData>
</tem:responseDA>

现在我需要根据字段中的“numofitems”对广告系列进行分组。

输出应该是..

<ns:FinalPricingQuoteResponse xmlns:ns="http://corp.alahli.com/middlewareservices/CreditVerificationService/1.1/" xmlns:tem="http://tempuri.org">
<ns:PricingQuoteOutput>
<ns:Campaigns>
<ns:campaignNumber>A</ns:campaignNumber>
<ns:downPayment>2000.00</ns:downPayment>
<ns:ballonPayment>4000.00</ns:ballonPayment>
</ns:Campaigns>
<ns:Campaigns>
<ns:campaignNumber>B</ns:campaignNumber>
<ns:downPayment>3000.00</ns:downPayment>
<ns:ballonPayment>5000.00</ns:ballonPayment>
</ns:Campaigns>
</ns:PricingQuoteOutput>
</ns:FinalPricingQuoteResponse>

请帮助我为XSLT1.0进行muenchian分组。 提前谢谢..

的问候, Inian

1 个答案:

答案 0 :(得分:0)

不确定您对Münchian分组的意思。据推测,每次都会出现3个列表(可变数量的条目,您可以这样做:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
        xmlns:ns="http://corp.alahli.com/middlewareservices/CreditVerificationService/1.1/"
        xmlns:tem="http://tempuri.org"
        exclude-result-prefixes="xd"
        version="1.0">

        <xsl:output indent="yes" method="xml"/>

        <xsl:template match="/">
            <ns:FinalPricingQuoteResponse>
                <xsl:apply-templates select="tem:responseDA/tem:outputData/tem:dictionary" />
            </ns:FinalPricingQuoteResponse>
        </xsl:template>

        <xsl:template match="tem:dictionary">
            <ns:PricingQuoteOutput>
                <xsl:apply-templates select="tem:list[position()=1]" />
            </ns:PricingQuoteOutput>
        </xsl:template>

        <xsl:template match="tem:list[position()=1]">
            <xsl:apply-templates select="tem:item/tem:field[@name='DOWNPAYMENT']" />
        </xsl:template>

        <xsl:template match="tem:field[@name='DOWNPAYMENT']">
            <xsl:variable name="pos" select="position()" />
            <ns:Campaigns>
                <ns:campaignNumber><xsl:value-of select="/tem:responseDA/tem:outputData/tem:dictionary/tem:list[position()=2]/tem:item[position()=$pos]/tem:field"/></ns:campaignNumber>
                <ns:downPayment><xsl:value-of select="."/></ns:downPayment>
                <ns:ballonPayment><xsl:value-of select="../../../tem:list[position()=3]/tem:item[position()=$pos]/tem:field"/></ns:ballonPayment>
            </ns:Campaigns>
        </xsl:template>
    </xsl:stylesheet>

注意两个不同的XPath(选择一个并坚持使用)来选择匹配的字段。当然,这将取决于列表如何出现的顺序,这可能是脆弱的。所以一个稍微强大的版本将是:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
        xmlns:ns="http://corp.alahli.com/middlewareservices/CreditVerificationService/1.1/"
        xmlns:tem="http://tempuri.org"
        exclude-result-prefixes="xd"
        version="1.0">

        <xsl:output indent="yes" method="xml"/>

        <xsl:template match="/">
            <ns:FinalPricingQuoteResponse>
                <xsl:apply-templates select="tem:responseDA/tem:outputData/tem:dictionary" />
            </ns:FinalPricingQuoteResponse>
        </xsl:template>

        <xsl:template match="tem:dictionary">
            <ns:PricingQuoteOutput>
                <xsl:apply-templates select="tem:list[tem:item/tem:field[@name='DOWNPAYMENT']]" />
            </ns:PricingQuoteOutput>
        </xsl:template>

        <xsl:template match="tem:list[tem:item/tem:field[@name='DOWNPAYMENT']]">
            <xsl:apply-templates select="tem:item/tem:field[@name='DOWNPAYMENT']" />
        </xsl:template>

        <xsl:template match="tem:field[@name='DOWNPAYMENT']">
            <xsl:variable name="pos" select="position()" />
            <ns:Campaigns>
                <ns:campaignNumber><xsl:value-of select="/tem:responseDA/tem:outputData/tem:dictionary/tem:list[tem:item/tem:field[@name='CAMPAIGNCODE']]/tem:item[position()=$pos]/tem:field"/></ns:campaignNumber>
                <ns:downPayment><xsl:value-of select="."/></ns:downPayment>
                <ns:ballonPayment><xsl:value-of select="../../../tem:list[tem:item/tem:field[@name='BALLOONPAYMENT']]/tem:item[position()=$pos]/tem:field"/></ns:ballonPayment>
            </ns:Campaigns>
        </xsl:template>
    </xsl:stylesheet>

希望有帮助并随意接受答案