请使用xslt1.0指导我根据属性值进行muenchian分组

时间:2014-04-22 08:16:29

标签: xslt

我想使用muenchian分组对元素进行分组。我正在使用xslt 1.0你能不能指导我......

xml如下所示..

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

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

现在我需要根据&#34; numofitems&#34;对广告系列进行分组。在列表的属性中。 输出应该是..

 <Campaigns>
    <campaignNumber>A</ns:campaignNumber>
    <downPayment>2000.00</ns:downPayment>
    <ballonPayment>4000.00</ns:ballonPayment>
    </Campaigns>
    <Campaigns>
    <campaignNumber>B</ns:campaignNumber>
    <downPayment>3000.00</ns:downPayment>
    <ballonPayment>5000.00</ns:ballonPayment>
    </Campaigns> 

我已经尝试但是它无效...

<xsl:template match="/">
<xsl:apply-templates select="responseDA" />
</xsl:template>

<xsl:key name="camp" match="//list/@numOfItems" use="field" />

<xsl:template match="responseDA">       
<xsl:for-each  select="list/@numOfItems[generate-id(.)=generate-id(key(&apos;camp&apos;,field)[1])]">
<Campaigns>
<campaignNumber>
<xsl:value-of select="item/field[@name=&apos;CAMPAIGNCODE&apos;]" />
</campaignNumber>
<xsl:for-each xmltem="http://tempuri.org" select="key(&apos;camp&apos;,field)">
<downPayment>
<xsl:value-of select="item/field[@name=&apos;DOWNPAYMENT&apos;]" />
</downPayment>
<ballonPayment>
<xsl:value-of select="item/field[@name=&apos;BALLOONPAYMENT&apos;]" />
</ballonPayment>
</xsl:for-each>
</Campaigns>
</xsl:for-each>     
</xsl:template>

请纠正我在哪里做错了......

的问候, Inian

1 个答案:

答案 0 :(得分:0)

这对我来说并不像是需要Muenchian分组的问题。您只想为列表中的(任何一个)中的每个item创建一个输出“行”,其值来自其他列表中相应的位置

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent="yes" />

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="//list[1]/item"/>
    </root>
  </xsl:template>

  <xsl:template match="item">
    <xsl:variable name="mypos" select="position()"/>
    <xsl:variable name="fields" select="//list/item[$mypos]/field"/>
    <Campaigns>
      <campaignNumber>
        <xsl:value-of select="$fields[@name='CAMPAIGNCODE']"/>
      </campaignNumber>
      <downPayment>
        <xsl:value-of select="$fields[@name='DOWNPAYMENT']"/>
      </downPayment>
      <ballonPayment>
        <xsl:value-of select="$fields[@name='BALLOONPAYMENT']"/>
      </ballonPayment>
    </Campaigns>
  </xsl:template>
</xsl:stylesheet>

在样本输入XML上运行时,会生成您请求的输出:

<?xml version="1.0"?>
<root>
  <Campaigns>
    <campaignNumber>A</campaignNumber>
    <downPayment>2000.00</downPayment>
    <ballonPayment>4000.00</ballonPayment>
  </Campaigns>
  <Campaigns>
    <campaignNumber>B</campaignNumber>
    <downPayment>3000.00</downPayment>
    <ballonPayment>5000.00</ballonPayment>
  </Campaigns>
</root>

(包装在一个根级元素中,以使其格式良好的XML)。

您在评论中说您需要“对广告系列进行排序” - 您可以在apply-templates点进行排序,记住上面给出的模板规则不关心哪个正在处理的行项目,只处理它来自同一行的所有项目。

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

但您还需要更改mypos变量的定义:

  <xsl:template match="item">
    <xsl:variable name="mypos" select="count(preceding-sibling::item) + 1"/>

因为position()现在会以排序顺序为您提供位置,而您需要原始文档顺序中的位置。

相关问题