xslt 1.0-在XSLT1.0中使用muenchian基于字段值对元素进行分组



请求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>

现在,我需要根据现场的"数字项目"对活动进行分组。

输出应为.

<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

不知道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>

希望这有帮助,并随时接受答案

最新更新