使用XSLT从XML到多个节点的多个属性进行分组

时间:2016-12-15 11:17:36

标签: xml xslt xpath

我有以下XML。

如何按xslt 1.0中的组名对元素进行分组?我需要根据communs_params地址,价格和增值税对值进行分组。其他参数(类型)必须分组在different_params

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<productsGrouppe>
 <product>
  <adressParam> 
   <parameter name="adress" >
     <value>street 1 </value>
   </parameter>
  </adressParam> 
  <priceParam>
   <parameter name="price" >
     <value>100 EUR </value>
   </parameter>
   <parameter name="vat" >
     <value> 1 </value>
   </parameter>
  </priceParam> 
  <deliveryParam>
    <parameter name="type" >
     <value> post </value>
   </parameter>
  </deliveryParam>
 </product>
 <product>
  <adressParam> 
   <parameter name="adress" >
     <value>street 2 </value>
   </parameter>
  </adressParam> 
  <priceParam>
   <parameter name="price" >
     <value>200 EUR </value>
   </parameter>
   <parameter name="vat" >
     <value> 2 </value>
   </parameter>
  </priceParam> 
  <deliveryParam>
    <parameter name="type" >
     <value> E-mail </value>
   </parameter>
  </deliveryParam>
 </product>
<product>
  <adressParam> 
   <parameter name="adress" >
     <value>street 1 </value>
   </parameter>
  </adressParam> 
  <priceParam>
   <parameter name="price" >
     <value>100 EUR </value>
   </parameter>
   <parameter name="vat" >
     <value> 1 </value>
   </parameter>
  </priceParam> 
  <deliveryParam>
    <parameter name="type" >
     <value> selfcollectors </value>
   </parameter>
  </deliveryParam>
 </product>
</productsGrouppe>

预期结果:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<productsGrouppe>
 <product>
   <commun_params>
     <value>street 1 </value>
     <value>100 EUR </value>
     <value> 1 </value>
   </commun_params>
   <different_params>
     <value> post </value>
      <value> selfcollectors </value>
   </different_params>
 </product>
</productsGrouppe>

1 个答案:

答案 0 :(得分:0)

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

    <xsl:key name="product_adress" match="adressParam/parameter[@name='adress']/value" use="."/>

    <xsl:variable name="priceparam_price" select="priceParam/parameter[@name='price']/value"></xsl:variable>
    <xsl:variable name="priceparam_vat" select="priceParam/parameter[@name='vat']/value"></xsl:variable>
    <xsl:variable name="adressParam_adress" select="adressParam/parameter[@name='adress']/value"></xsl:variable>

    <xsl:template match="productsGrouppe">
        <product>
            <xsl:apply-templates select="product/adressParam/parameter[@name='adress']/value[generate-id(.)=generate-id(key('product_adress',.)[1])]"/>
        </product>
    </xsl:template>

    <xsl:template match="product">

            <xsl:for-each select="key('product_adress', $adressParam_adress)">
                <commun_params>
                    <value><xsl:value-of select="$adressParam_adress" /></value>
                    <value><xsl:value-of select="$priceparam_price" /></value>
                    <value><xsl:value-of select="$priceparam_vat" /></value>
                </commun_params>
            </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

这就是结果:

    街1街2号