XSL:搜索属性等于当前属性之一 属性 的元素

XSL: Search for an element having an attribute equals to one property of the current attribute

我有一个 XML 目前看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<RootConfig>
  <RandomNode RefId="519263a7-e01e-4cc8-911e-7660dca717bf">
    <Id>101010101010</Id>
  </RandomNode>

  <Item Value="519263a7-e01e-4cc8-911e-7660dca717bf;bf139890-2f7c-4784-8041-68aa5fe7beb1" Type="SomeUniqueType" />
  <Item Value="519263a7-e01e-4cc8-911e-7660dca717bf;5fb8bea0-c79a-4a26-a532-4df59543bc5c" Type="SomeUniqueType" />
  <Item Value="519263a7-e01e-4cc8-911e-7660dca717bf;4f01116a-06f8-4af3-9f4a-87c658eb8008" Type="SomeUniqueType" />
</RootConfig>

在每个项目中,该值引用另一个节点,并为该给定节点提供一个引用。

我必须将其更改为以这样的文件结尾:

<?xml version="1.0" encoding="utf-8"?>
<RootConfig>
  <RandomNode RefId="519263a7-e01e-4cc8-911e-7660dca717bf">
    <Id>101010101010</Id>
  </RandomNode>
  <Item Reference="bf139890-2f7c-4784-8041-68aa5fe7beb1" Provider-Id="101010101010" />
  <Item Reference="5fb8bea0-c79a-4a26-a532-4df59543bc5c" Provider-Id="101010101010" />
  <Item Reference="4f01116a-06f8-4af3-9f4a-87c658eb8008" Provider-Id="101010101010" />
</RootConfig>

目前,我有以下 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>


  <xsl:template match="*[@Type='SomeUniqueType']">
    <Item>
      <xsl:call-template name="provided-reference">
        <xsl:with-param name="reference" select="@Value"/>
      </xsl:call-template>
    </Item>
  </xsl:template>

  <xsl:template name="provided-reference">
    <xsl:param name="reference"/>
    <xsl:attribute name="Reference">
      <xsl:value-of select="substring-after($reference, ';')"/>
    </xsl:attribute>
    <xsl:attribute name="Provider-Id">
      <xsl:value-of select="substring-before($reference, ';')"/>
    </xsl:attribute>
  </xsl:template>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

但我找不到如何查找从 substring-before 中获得的值,然后获取 Id 属性的值。

知道怎么做吗?

我已经很久没有做任何 XSLT 了,不是吗?

<xsl:value-of select="//RandomNode[@RefId = substring-before($reference, ';')]" />

我想如果您尝试在 $reference 不在范围内的地方执行此操作,那么您将遇到一个范围问题,其中 .指向方括号的上下文。在这种情况下,您必须这样做:

<xsl:variable name="this" value="." />
<xsl:value-of select="//RandomNode[@RefId = substring-before($this/@Value, ';')]" />

假设您的上下文是一个项目节点。

也许为此使用键会更好,尤其是当您要使用许多不同的 RandomNode 元素进行查找时。此外,不需要命名模板或参数。

XSLT 样式表

如果确实需要,Item 的模板匹配当然可以更改为 match="*[@Type='SomeUniqueType']"

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="ref-id-to-id" match="RandomNode" use="@RefId"/>

  <xsl:template match="Item">
      <xsl:copy>
          <xsl:attribute name="Value">
              <xsl:value-of select="substring-after(@Value, ';')"/>
          </xsl:attribute>
          <xsl:attribute name="Provider-Id">
              <xsl:value-of select="key('ref-id-to-id',substring-before(@Value,';'))/Id"/>
          </xsl:attribute>
      </xsl:copy>
  </xsl:template>


    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XML输出

<?xml version="1.0" encoding="utf-8"?>
<RootConfig>
   <RandomNode RefId="519263a7-e01e-4cc8-911e-7660dca717bf">
      <Id>101010101010</Id>
   </RandomNode>
   <Item Value="bf139890-2f7c-4784-8041-68aa5fe7beb1" Provider-Id="101010101010"/>
   <Item Value="5fb8bea0-c79a-4a26-a532-4df59543bc5c" Provider-Id="101010101010"/>
   <Item Value="4f01116a-06f8-4af3-9f4a-87c658eb8008" Provider-Id="101010101010"/>
</RootConfig>

顺便说一下,对于你的下一个问题:一个更明智的测试用例应该包含 Item 元素,这些元素引用了 不同的 提供者,具有不同的 ID。