Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT基于另一个值添加属性值_Xml_Xslt_Xpath_Xslt 2.0 - Fatal编程技术网

Xml XSLT基于另一个值添加属性值

Xml XSLT基于另一个值添加属性值,xml,xslt,xpath,xslt-2.0,Xml,Xslt,Xpath,Xslt 2.0,如何基于另一个值添加值 <?xml version="1.0" encoding="UTF-8"?> <items> <item id="A1" quantity="5"> <info type="ram x1" import="CA" /> </item> <item id="A2" quantity="3"> <info type="ram x1" import="SA"

如何基于另一个值添加值

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item id="A1" quantity="5">
      <info type="ram x1" import="CA" />
   </item>
   <item id="A2" quantity="3">
      <info type="ram x1" import="SA" />
   </item>
   <item id="A3" quantity="10">
      <info type="ram x2" import="AU" />
   </item>
</items>
尝试让每组先获取数量,看看是否有效

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="html" indent="yes" />
   <xsl:template match="items">
      <xsl:for-each-group select="item" group-by="info/@type">
         <xsl:value-of select="sum(@quantity)" />
      </xsl:for-each-group>
   </xsl:template>
</xsl:stylesheet>

使用
current-group()
函数,即:

<xsl:value-of select="sum(current-group()/@quantity)" />

@Kirill Polishchuck已经给出了一个很好的答案,我想添加一个完整的样式表来说明这一点

它以您所展示的方式输出XML格式。除了使用
current-grouping()
,还有一个有趣的应用程序
current-grouping-key()
,它检索导致当前项目分组在一起的值

您已将
xsl:output方法
指定为HTML,但预期的输出类似于XML。因此,我将其更改为输出XML

样式表

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

 <xsl:template match="items">
  <xsl:copy>
     <xsl:for-each-group select="item" group-by="info/@type">
        <details>
           <xsl:attribute name="type">
              <xsl:value-of select="current-grouping-key()"/>
           </xsl:attribute>
           <xsl:attribute name="quantity">
              <xsl:value-of select="sum(current-group()/@quantity)" />
           </xsl:attribute>
        </details>
     </xsl:for-each-group>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<items>
  <details type="ram x1" quantity="8"/>
  <details type="ram x2" quantity="10"/>
</items>

输出

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

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

 <xsl:template match="items">
  <xsl:copy>
     <xsl:for-each-group select="item" group-by="info/@type">
        <details>
           <xsl:attribute name="type">
              <xsl:value-of select="current-grouping-key()"/>
           </xsl:attribute>
           <xsl:attribute name="quantity">
              <xsl:value-of select="sum(current-group()/@quantity)" />
           </xsl:attribute>
        </details>
     </xsl:for-each-group>
  </xsl:copy>
 </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<items>
  <details type="ram x1" quantity="8"/>
  <details type="ram x2" quantity="10"/>
</items>

更简洁(但更复杂)的版本使用所谓的属性值模板:

<xsl:for-each-group select="item" group-by="info/@type">
   <details type="{current-grouping-key()}" quantity="{sum(current-group()/@quantity)}"/>
</xsl:for-each-group>