简体   繁体   English

XSLT 1.0 不同节点和元素的分组键

[英]XSLT 1.0 Grouping Key for different Nodes and Elements

I am looking at the Muenchian Grouping.我在看Muenchian Grouping。 I tried finding examples that are similar to my xml but can't find any.我尝试查找与我的 xml 类似的示例,但找不到任何示例。 Most of the examples are well structured while mine is confusing.大多数示例结构良好,而我的示例令人困惑。

Here's a shortened version of my XML (note that I can't change the XML structure because it's a standard thing and out of my hands), and I'm using XSLT 1 because the system only supports that version now.这是我的 XML 的缩短版本(请注意,我无法更改 XML 结构,因为它是标准的东西并且不在我的手中),我正在使用 Z139E293A7146F223CD247C6BE041 版本,因为系统现在只支持该版本

<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>

The output that I desire is:我想要的 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>
       <role>Contact</role>
   </party>

Now the id attribute is being used as a value for the references element.现在 id 属性被用作引用元素的值。 And the tag in the output can be either creator, contact, or whatever is inside element if it's under an associatedParty element. output 中的标签可以是创建者、联系人或任何内部元素(如果它位于关联方元素下)。

I'm stuck with creating the key to group them from their id/references attribute.我坚持创建密钥以从它们的 id/references 属性对它们进行分组。 As far as I see the examples using xsl:key is only for nodes that have the same name, and the example I posted have different node names.据我所见,使用 xsl:key 的示例仅适用于具有相同名称的节点,而我发布的示例具有不同的节点名称。 Any help would be appreciated!!!!任何帮助,将不胜感激!!!!

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 :当应用于提供的 XML 文档时

<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: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM