重新链接跳过元素的子元素的 parentID

Relink the parentID of the child of skipped element

我想使用 xslt 更改已跳过元素的子元素的 ParentId。我的输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
    <Board>
        <Name>President</Name>
        <Id>ABCDE</Id>
        <ParentId></ParentId>
        <General>
            <Name>President</Name>
            <Description>Top level of the Hierarchy</Description>
            <Template>LEVEL1</Template>
        </General>
    </Board>
    <Board>
        <Name>VP</Name>
        <Id>EFGHI</Id>
        <ParentId>ABCDE</ParentId>
        <General>
            <Name>VP</Name>
            <Description>Below the President</Description>
            <Template>LEVEL2</Template>
        </General>
    </Board>
    <Board>
        <Name>Department_Heads</Name>
        <Id>JKLMN</Id>
        <ParentId>EFGHI</ParentId>
        <General>
            <Name>Department_Heads</Name>
            <Description>Reports to the VP</Description>
            <Template>LEVEL3</Template>
        </General>
    </Board>
    <Board>
        <Name>Supervisors</Name>
        <Id>OPQRS</Id>
        <ParentId>JKLMN</ParentId>
        <General>
            <Name>Supervisors</Name>
            <Description>Reports to the Reports to Dep Heads</Description>
            <Template>LEVEL4</Template>
        </General>
    </Board>
    <Board>
        <Name>Employees</Name>
        <Id>TUVWX</Id>
        <ParentId>OPQRS</ParentId>
        <General>
            <Name>Employees</Name>
            <Description>Reports to the Reports to Dep Heads</Description>
            <Template>LEVEL5</Template>
        </General>
    </Board>
</Hierarchy>

预期输出文件:

<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
  <Board>
    <Name>President</Name>
    <Description>Top level of the Hierarchy</Description>
    <Template>LEVEL1</Template>
      <Id>ABCDE</Id>
      <ParentId></ParentId>
    <Board>
      <Name>VP</Name>
      <Description>Below the President</Description>
      <Template>LEVEL2</Template>
       <Id>EFGHI</Id>
        <ParentId>ABCDE</ParentId>
      <Board>
        <Name>Supervisors</Name>
        <Description>Reports to the Reports to Dep Heads</Description>
        <Template>LEVEL4</Template>
        <Id>OPQRS</Id>
        <ParentId>EFGHI</ParentId>
        <Board>
          <Name>Employees</Name>
          <Description>Reports to the Reports to Dep Heads</Description>
          <Template>LEVEL5</Template>
           <Id>TUVWX</Id>
        <ParentId>OPQRS</ParentId>
        </Board>
      </Board>
    </Board>
  </Board>
</Hierarchy>

现在在我提出的另一个问题的帮助下,我找到了一种跳过元素的方法。下面的问题 link :

但是如果您在预期的输出中观察到,Supervisors 的 ParentId 更改为 VP 的 Id,从而完全跳过元素“Depeartment Heads”。这可能吗?有人可以为此提供可能的解决方案吗?提前致谢。

我不明白为什么你需要 Board 元素来继续携带它们父元素的 Id,在你将结构更改为适当的层次结构之后。

收到以下格式结果的任何人:

<Hierarchy>
    <Board>
        <Name>President</Name>
        <Description>Top level of the Hierarchy</Description>
        <Template>LEVEL1</Template>
        <Id>ABCDE</Id>
        <Board>
            <Name>VP</Name>
            <Description>Below the President</Description>
            <Template>LEVEL2</Template>
            <Id>EFGHI</Id>
            <Board>
                <Name>Supervisors</Name>
                <Description>Reports to the Reports to Dep Heads</Description>
                <Template>LEVEL4</Template>
                <Id>OPQRS</Id>
                <Board>
                    <Name>Employees</Name>
                    <Description>Reports to the Reports to Dep Heads</Description>
                    <Template>LEVEL5</Template>
                    <Id>TUVWX</Id>
                </Board>
            </Board>
        </Board>
    </Board>
</Hierarchy>

很容易看出Board的父级SupervisorsBoardVP,它的Id值为EFGHI 可以使用表达式 parent::Id 轻松获得。在单独的 ParentId 元素中复制此值是完全多余的。

不过,如果你想要这种不必要的复杂化,你可以这样做:

XSLT 1.0

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

<xsl:key name="children" match="Board" use="ParentId" />

<xsl:template match="/Hierarchy">
    <xsl:copy>
        <xsl:apply-templates select="Board[General/Template='LEVEL1']"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Board">
    <xsl:param name="parentId" select="ParentId"/>
    <xsl:copy>
        <xsl:copy-of select="General/*"/>
        <xsl:copy-of select="Id"/>
        <ParentId>
            <xsl:value-of select="$parentId"/>
        </ParentId>
        <xsl:apply-templates select="key('children', Id)"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Board[General/Template='LEVEL3']">
    <xsl:apply-templates select="key('children', Id)">
        <xsl:with-param name="parentId" select="ParentId"/>
    </xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>

请注意,这假设您不会连续跳过两个或多个级别。