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

发布于 2024-11-29 08:06:22 字数 1412 浏览 1 评论 0原文

我正在看慕尼黑分组。我尝试查找与我的 xml 类似的示例,但找不到任何示例。大多数示例结构良好,而我的示例则令人困惑。

这是我的 XML 的缩短版本(请注意,我无法更改 XML 结构,因为它是标准的东西,不由我掌控),我使用的是 XSLT 1,因为系统现在只支持该版本。

<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 属性被用作引用元素的值。输出中的标记可以是创建者、联系人,或者元素内部的任何内容(如果位于关联方元素下)。

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

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.

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>

The output that I desire is:

   <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. And the tag in the output can be either creator, contact, or whatever is inside element if it's under an associatedParty element.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2024-12-06 08:06:22

此转换

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

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>
野稚 2024-12-06 08:06:22

您可以使用此模板:

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

输出:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文