XSLT 1.0 Grouping Key for different Nodes and Elements

。_饼干妹妹 提交于 2019-12-06 04:45:11

This transformation:

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

 <xsl:key name="kRefByVal" match="references"
  use="."/>

 <xsl:key name="kCreatorById" match="creator"
  use="@id"/>

 <xsl:key name="kRoleNameByRef"
      match="*[not(self::associatedParty
                  or
                   self::creator
                   )
              ]"
      use="references"/>

 <xsl:key name="kAssocByRef"
      match="associatedParty"
      use="references"/>

 <xsl:template match="/">
  <xsl:variable name="vReferences" select=
   "*/*/references
         [generate-id()
         =
          generate-id(key('kRefByVal',.)[1])
         ]
   "/>

  <xsl:apply-templates select="$vReferences">
   <xsl:sort select="." data-type="number"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="references" priority="3">
  <party id="{.}">
    <xsl:copy-of select="key('kCreatorById',.)/*"/>
    <xsl:apply-templates select=
     "key('kCreatorById',.)"/>

    <xsl:apply-templates select=
     "key('kRoleNameByRef',.)"/>

    <xsl:copy-of select="key('kAssocByRef',.)/role"/>
  </party>
 </xsl:template>

 <xsl:template match="*[not(self::associatedParty)]">
  <role>
   <xsl:value-of select="name()"/>
  </role>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<object>
    <creator id="123">
        <name>ABC</name>
        <city>Hamilton</city>
    </creator>
    <creator>
        <references>456</references>
    </creator>
    <contact>
        <references>123</references>
    </contact>
    <creator id="456">
        <name>XYZ</name>
        <city>New York</city>
    </creator>
    <associatedParty>
        <references>123</references>
        <role>Sponsor</role>
    </associatedParty>
</object>

produces the wanted, correct result:

<party id="123">
   <name>ABC</name>
   <city>Hamilton</city>
   <role>creator</role>
   <role>contact</role>
   <role>Sponsor</role>
</party>
<party id="456">
   <name>XYZ</name>
   <city>New York</city>
   <role>creator</role>
</party>

You can use this template:

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

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

  <xsl:template match="/">
    <xsl:apply-templates select="//creator[not(references)]"/>
  </xsl:template>

  <xsl:template match="creator">
    <party id="{@id}">
      <xsl:copy-of select="name"/>
      <xsl:copy-of select="city"/>
      <role>Creator</role>
      <xsl:apply-templates select="../*[not(self::creator) and references = current()/@id]"/>
    </party>

  </xsl:template>

  <xsl:template match="associatedParty" priority="1">
    <xsl:copy-of select="role"/>
  </xsl:template>

  <xsl:template match="*[references]">
    <role>
      <xsl:value-of select="name()"/>
    </role>
  </xsl:template>

</xsl:stylesheet>

Output:

<party id="123">
  <name>ABC</name>
  <city>Hamilton</city>
  <role>Creator</role>
  <role>contact</role>
  <role>Sponsor</role>
</party>
<party id="456">
  <name>XYZ</name>
  <city>New York</city>
  <role>Creator</role>
</party>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!