合并多个元素的值并获取属性字段的平均值

时间:2015-01-08 15:15:34

标签: xml xslt xml-parsing xslt-1.0

我需要合并多个元素的值并获取属性字段的平均值。

[INPUT]

<xml>
   <characters>
       <char a="a1" b="b1" y="y1" z="z1"  start="1" weight="100">F</char>
       <char a="a2" b="b2" y="y2" z="z2"  start="0" weight="80">r</char>
       <char a="a3" b="b3" y="y3" z="z3"  start="0" weight="80">o</char>
       <char a="a4" b="b4" y="y4" z="z4"  start="0" weight="100">m</char>
       <char a="a5" b="b5" y="y5" z="z5"> </char>
       <char a="a6" b="b6" y="y6" z="z6"  start="1" weight="100">a</char>
       <char a="a7" b="b7" y="y7" z="z7"  start="0" weight="80">n</char>
       <char a="a8" b="b8" y="y8" z="z8"  start="0" weight="80">d</char>
       <char a="a9" b="b9" y="y9" z="z9"> </char>
   </characters>
</xml>

[OUTPUT]

<xml>
   <data>
       <word>
         <value>From</value>
         <coordinates>a1 b1 y4 z4</coordinates>
         <avgconfidence>90</avgconfidence>
        </word>

        <word>
            <value>and</value>
            <coordinates>a6 b6 y9 z9</coordinates>
            <avgconfidence>90</avgconfidence>
         </word>
  <data>
</xml>

我在@michael.hor257k中提出了older thread的建议,下面写了一个部分XSL 。我尝试过使用xsl:choose。它没有产生预期的结果

[XSLT]

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

    <xsl:template match="/">
        <data>
            <xsl:for-each select="characters">
               <word>
                    <value>
                        <xsl:for-each select="char">
                            <xsl:variable name="rec" select="." />
                            <xsl:if test="$rec/@start='1'">
                                <xsl:text>  <xsl:value-of select="$rec" /></xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </value>

                     <coordinates>
            <xsl:value-of select="char[1]/@a"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="char[1]/@b"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="char[last()]/@y"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="char[last()]/@z"/>
        </coordinates>
                    <avgWeight>
                        <xsl:value-of select="sum(characters/char/@weight) div count(characters/char) "/>
                    </avgWeight>
                </word>

            </xsl:for-each>
        </data>
    </xsl:template>
</xsl:stylesheet>

请帮忙。

1 个答案:

答案 0 :(得分:0)

有趣的问题 - 您的部分XSL适用于单个单词,但您需要一种方法将字符分成单独的单词并分别处理每个单词。我认为它是一个分组任务,其中一个&#34;字&#34;由char[@start='1']元素及其后续char[@start='0']元素组成。因此,您可以使用将每个非字首字母char与其单词的开头(即最近的start='1'兄弟姐妹)相关联来接近它

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

    <xsl:key name="wordChars" match="char[@start='0']"
             use="generate-id(preceding-sibling::char[@start='1'][1])" />

    <xsl:template match="/">
        <xml>
           <data>
               <xsl:apply-templates select="xml/characters/char[@start='1']" />
           </data>
        </xml>
    </xsl:template>

    <xsl:template match="char">
        <!-- all the chars in this word - . is the initial letter, the key gives the
             rest of the word -->
        <xsl:variable name="word" select=". | key('wordChars', generate-id())" />

        <word>
            <value>
                <xsl:for-each select="$word"><xsl:value-of select="."/></xsl:for-each>
            </value>
            <coordinates>
                <xsl:value-of select="concat(
                     $word[1]/@a, ' ', $word[1]/@b, ' ',
                     $word[last()]/@y, ' ', $word[last()]/@z)" />
            </coordinates>
            <avgconfidence>
                <xsl:value-of select="sum($word/@weight) div count($word)" />
            </avgconfidence>
        </word>
    </xsl:template>
</xsl:stylesheet>

当您在样本输入上运行时,会生成

<xml>
  <data>
    <word>
      <value>From</value>
      <coordinates>a1 b1 y4 z4</coordinates>
      <avgconfidence>90</avgconfidence>
    </word>
    <word>
      <value>and</value>
      <coordinates>a6 b6 y8 z8</coordinates>
      <avgconfidence>86.6666666666667</avgconfidence>
    </word>
  </data>
</xml>

我相信这是正确的答案(最后的坐标是y8 z8而不是y9 z9和置信度86 2 / 3 而不是第二个词比90更好。