【问题标题】:XSLT 2.0 - Move existing child nodes to newly created NodeXSLT 2.0 - 将现有子节点移动到新创建的节点
【发布时间】:2018-01-31 12:08:37
【问题描述】:
<xsl:template match="BaseNode">
      <xsl:copy>
         <xsl:copy-of select="@*" />
         <xsl:apply-templates/>
         <None attr1="0" attt2="1" attr2="0">
         <xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input A']"/>
         <xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input B']"/>
         </None>
      </xsl:copy>        
</xsl:template>

上面是我尝试使用的 XSLT 将现有子节点移动到新创建的节点。 该规则正在创建新节点并将节点复制为子节点。 问题是我在父节点中仍然有旧元素。 我想要的是剪切和粘贴操作,而不是复制粘贴。 预先感谢您的帮助。

输入 XML

<ParentNode attr1="1" attr2="2">
    <BasicInt attr1="1" attr2="2"/>
    <BasicInt attr1="1" attr2="2"/>
    <BasicDouble attr1="1" attr2="2"/>
    <BasicEnum attr1="1" attr2="2"/>
        <Pair Key="0" Value="a"/>
        <Pair Key="1" Value="b"/>
        <Pair Key="2" Value="c"/>
        <Pair Key="3" Value="d"/>
    </BasicEnum>
    <BasicDouble attr1="1" attr2="2"/>
    <BasicDouble attr1="1" attr2="2"/>
</ParentNode>

期望的输出

<ParentNode attr1="1" attr2="2">
    <BasicInt attr1="1" attr2="2"/>
    <BasicInt attr1="1" attr2="2"/>
    <BasicDouble attr1="1" attr2="2"/>
    <BasicEnum attr1="1" attr2="2"/>
        <Pair Key="0" Value="a"/>
        <Pair Key="1" Value="b"/>
        <Pair Key="2" Value="c"/>
        <Pair Key="3" Value="d"/>
    </BasicEnum>
    <NewElement>
        <BasicDouble attr1="1" attr2="2"/>
        <BasicDouble attr1="1" attr2="2"/>
    </NewElement>
</ParentNode>

使用我的规则获得输出

<ParentNode attr1="1" attr2="2">
    <BasicInt attr1="1" attr2="2"/>
    <BasicInt attr1="1" attr2="2"/>
    <BasicDouble attr1="1" attr2="2"/>
    <BasicEnum attr1="1" attr2="2"/>
        <Pair Key="0" Value="a"/>
        <Pair Key="1" Value="b"/>
        <Pair Key="2" Value="c"/>
        <Pair Key="3" Value="d"/>
    </BasicEnum>
    <BasicDouble attr1="1" attr2="2"/>
    <BasicDouble attr1="1" attr2="2"/>
    <NewElement>
        <BasicDouble attr1="1" attr2="2"/>
        <BasicDouble attr1="1" attr2="2"/>
    </NewElement>

【问题讨论】:

  • 请共享输入 XML 和所需的输出 XML,以便共享的 XSLT 可用于重现问题。
  • 只是猜测:尝试删除 &lt;xsl:apply-templates/&gt; 元素。
  • 通过删除 我需要的其余子节点不会被复制。
  • 如果您使用的是 XSLT 1.0 处理器(并且不打算升级),请不要标记问题 XSLT 2.0。

标签: xml xslt xslt-2.0


【解决方案1】:

请给出 XML 示例以获得更清楚的说明。 你可以试试这个:

<xsl:template match="BaseNode">
      <xsl:copy>
         <xsl:copy-of select="@*" />
         <xsl:apply-templates select="node() except (//BaseNode/BasicDouble[@Descr = 'Input A'],//BaseNode/BasicDouble[@Descr = 'Input B'])"/>
         <None attr1="0" attt2="1" attr2="0">
         <xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input A']"/>
         <xsl:apply-templates select="//BaseNode/BasicDouble[@Descr = 'Input B']"/>
         </None>
      </xsl:copy>        
</xsl:template>

【讨论】:

  • 如果我尝试这个,我得到以下错误: FatalError: javax.xml.transform.TransformerException: Extra非法令牌:'except', '(', '/', '/',跨度>
  • 如果错误是因为您的处理器在 XSLT 1.0 上工作,请尝试将上面的第 4 行替换为以下内容:
  • 非常感谢您的回复。这工作得很好。我认为我在网站上没有足够的声誉来接受答案。但我会投票
猜你喜欢
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
相关资源
最近更新 更多