xslt使用Munchian分组,键中包含多个元素

时间:2014-11-20 20:15:10

标签: xslt

输入XML:

<root>
    <contact>
      <forename>John</forename>
      <surname>Smith</surname>
      <charge>1</charge>
    </contact>
    <contact>
      <forename>John</forename>
      <surname>Smith</surname>
      <charge>2</charge>
    </contact>
    <contact>
      <forename>John</forename>
      <surname>Jones</surname>
      <charge>3</charge>
    </contact>
</root>

期望的输出(按姓氏,姓氏分组):

<root>
  <Person>
    <First>John</First>
    <Last>Jones</Last>
    <Charges>
        <Charge>3</Charge>
    </Charges>
  </Person>
  <Person>
    <First>John</First>
    <Last>Smith</Last>
    <Charges>
       <Charge>1</Charge>
       <Charge>2</Charge>
    </Charges>
  </Person>
</root>

XSL(将此处描述的Munchian方法Munchian Method用于密钥(forename,surname)中的多个元素:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="contacts-by-name" match="contact" use="concat(forename, surname)" />
<xsl:template match="records">
 <root>
    <xsl:for-each select="contact[count(. | key('contacts-by-name', concat(forename, surname))[1]) = 1]">
      <xsl:sort select="concat(forename, surname)" />
      <Person>
        <First><xsl:value-of select="forename"/></First>
        <Last><xsl:value-of select="surname"/></Last>
        <Charges>
        <xsl:for-each select="key('contacts-by-name', concat(forename, surname))">
                <xsl:copy-of select="Charge"/>
        </xsl:for-each>
        </Charges>
      </Person>
    </xsl:for-each>
 </root>
</xsl:template>
</xsl:stylesheet>

实际输出(注意费用为空):

<root>
  <Person>
    <First>John</First>
    <Last>Jones</Last>
    <Charges/>
  </Person>
  <Person>
    <First>John</First>
    <Last>Smith</Last>
    <Charges/>
  </Person>
</root>

1 个答案:

答案 0 :(得分:0)

发现有一个错字... select =&#34; Charge&#34;而不是选择=&#34;充电&#34;。立即行动。