不同节点和元素的XSLT 1.0分组密钥 [英] XSLT 1.0 Grouping Key for different Nodes and Elements

查看:87
本文介绍了不同节点和元素的XSLT 1.0分组密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Muenchian分组.我尝试找到与xml类似但找不到的示例.大多数示例结构合理,而我却很困惑.

I am looking at the Muenchian Grouping. I tried finding examples that are similar to my xml but can't find any. Most of the examples are well structured while mine is confusing.

这是XML的简化版本(请注意,我无法更改XML结构,因为它是标准的东西,而且不方便使用),而我正在使用XSLT 1,因为系统现在仅支持该版本. /p>

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.

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

我想要的输出是:

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

现在,id属性被用作references元素的值.并且输出中的标记可以是创建者,联系人,也可以是元素中的任何东西(如果它在associatedParty元素下).

Now the id attribute is being used as a value for the references element. And the tag in the output can be either creator, contact, or whatever is inside element if it's under an associatedParty element.

我坚持创建密钥,以便根据其id/references属性对它们进行分组.据我所见,使用xsl:key的示例仅适用于具有相同名称的节点,而我发布的示例具有不同的节点名称.任何帮助将不胜感激!!!!!

I'm stuck with creating the key to group them from their id/references attribute. 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. Any help would be appreciated!!!!

推荐答案

此转换:

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

应用于提供的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>

产生想要的正确结果:

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

这篇关于不同节点和元素的XSLT 1.0分组密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆