如何采取平均数据和XML数据的规范化(how to take average and normali

2019-09-28 12:23发布

能否请你帮我制定一个XSL代码,可以做以下活动顺序:
1.两个元件的取平均
2.跨正常化记录的平均值。

当我在XSL的开发难倒是新增加的下一个元素的重用。 任何帮助,将不胜感激

例:
在以下每个记录的样本输入文件
1.平均=(C + d)/ 2
- 平均=(C + d)/ 2 =(12 + 12)/ 2 = 12,(8 + 12)/ 2 = 10 ....
2. avg_nom =平均/分钟(平均)
- avg_nom =平均/分钟(平均)= 12 /分(12,10,15,27)= 1.2

<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
    <a>no</a>
    <b>10</b>
    <c>12</c>
    <d>12</d>
</Results>
<Results>
    <a>no</a>
    <b>8</b>
    <c>8</c>
    <d>12</d>
</Results>
<Results>
    <a>no</a>
    <b>6</b>
    <c>10</c>
    <d>20</d>
</Results>
<Results>
    <a>yes</a>
    <b>23</b>
    <c>20</c>
    <d>34</d>
</Results>
 </top>

预期的结果应该是

<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
    <a>no</a>
    <b>10</b>
    <avg>12</avg>
    <avg_nom>1.2</avg_nom>
    <c>12</c>
    <d>12</d>
</Results>
<Results>
    <a>no</a>
    <b>8</b>
    <avg>10</avg>
    <avg_nom>1</avg_nom>
    <c>8</c>
    <d>12</d>
</Results>
<Results>
    <a>no</a>
    <b>6</b>
    <avg>15</avg>
    <avg_nom>1.5</avg_nom>
    <c>10</c>
    <d>20</d>
</Results>
<Results>
    <a>yes</a>
    <b>23</b>
    <avg>27</avg>
    <avg_nom>2.7</avg_nom>
    <c>20</c>
    <d>34</d>
</Results>
</top>

Answer 1:

随着XSLT 2.0,您可以使用

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

<xsl:output indent="yes"/>

<xsl:variable name="step1-result">
  <xsl:apply-templates select="top" mode="step1"/>
</xsl:variable>

<xsl:template match="@* | node()" mode="#all">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="c" mode="step1">
  <avg><xsl:value-of select="(. + ../d) div 2"/></avg>
  <xsl:next-match/>
</xsl:template>

<xsl:template match="/*">
  <xsl:copy>
    <xsl:variable name="min-avg" select="min($step1-result/top/Results/avg)"/>
    <xsl:apply-templates select="$step1-result/top/*">
      <xsl:with-param name="min-avg" tunnel="yes" select="$min-avg"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="avg">
  <xsl:param name="min-avg" tunnel="yes"/>
  <xsl:next-match/>
  <avg_nom><xsl:value-of select=". div $min-avg"/></avg_nom>
</xsl:template>

</xsl:stylesheet>

以下是上述的样式表的XSLT 1.0版本:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="step1-result-fragment">
  <xsl:apply-templates select="top" mode="step1"/>
</xsl:variable>

<xsl:variable name="step1-result" select="exsl:node-set($step1-result-fragment)"/>

<xsl:template match="@* | node()" name="identity">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | node()" mode="step1" name="step1-identity">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" mode="step1"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="c" mode="step1">
  <avg><xsl:value-of select="(. + ../d) div 2"/></avg>
  <xsl:call-template name="step1-identity"/>
</xsl:template>

<xsl:template match="/*">
  <xsl:copy>
    <xsl:variable name="min-avg">
      <xsl:for-each select="$step1-result/top/Results/avg">
        <xsl:sort select="." data-type="number"/>
        <xsl:if test="position() = 1">
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:apply-templates select="$step1-result/top/Results">
      <xsl:with-param name="min-avg" select="$min-avg"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="Results">
  <xsl:param name="min-avg"/>
  <xsl:copy>
    <xsl:apply-templates select="@* | node()">
      <xsl:with-param name="min-avg" select="$min-avg"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="avg">
  <xsl:param name="min-avg"/>
  <xsl:call-template name="identity"/>
  <avg_nom><xsl:value-of select=". div $min-avg"/></avg_nom>
</xsl:template>

</xsl:stylesheet>


文章来源: how to take average and normalize of xml data
标签: xml xslt