xslt:将子节点移动到父节点

xslt:将子节点移动到父节点,xslt,Xslt,我的目标是访问元素“p”(作为新的子元素)的某些属性,同时将原始内容的其余部分放到新的同级子元素“p1”中,然后在编辑完成后返回到原始结构 因此,原始节点如下所示: <p attr1="..." attr2="..." moreattr1="..." moreattr2="...">...content,more nodes,etc...</p> …内容、更多节点等 我的“可编辑”结构如下: <p> <attr1edit value="..."

我的目标是访问元素“p”(作为新的子元素)的某些属性,同时将原始内容的其余部分放到新的同级子元素“p1”中,然后在编辑完成后返回到原始结构

因此,原始节点如下所示:

<p attr1="..." attr2="..." moreattr1="..." moreattr2="...">...content,more nodes,etc...</p>

…内容、更多节点等

我的“可编辑”结构如下:

<p>
   <attr1edit value="...">
   <attr2edit value="...">
   <p1 moreattr1="..." moreattr2="...">...content,more nodes...</p1>
</p>

…内容,更多节点。。。

“p1”现在已包含前“p”的所有内容,但前导属性元素除外

当我试图回到原始结构时,我被困在这一点上:

我可以将属性(attr1、attr2)与其他属性一起放回“p1”中,但是我不知道如何将“p1”的全部内容交换回“p”并删除“p1”,或者删除“p”并将“p1”重命名为“p”(这将使节点向上移动一步)。 如何做到这一点? 非常感谢-Chris

样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="p">
  <xsl:copy>
    <xsl:apply-templates select="*[not(self::p1)] | p1/@*"/>
    <xsl:apply-templates select="p1/node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="p/*">
  <xsl:attribute name="{substring-before(name(), 'edit')}">
    <xsl:value-of select="@value"/>
  </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

转变

<p>
   <attr1edit value="..."/>
   <attr2edit value="..."/>
   <p1 moreattr1="..." moreattr2="...">...content,more nodes...</p1>
</p>

…内容,更多节点。。。

进入

…内容,更多节点


您使用哪个XSLT版本?你想在一个样式表中完成这一切吗?还是要进行链变换?我不清楚,如果最终结果不同,为什么需要可编辑结构。通常,如果您需要一个转换中的临时数据,您可以使用XSLT 2.0轻松地做到这一点,只要您使用模式分离转换步骤。亲爱的Martin,您是对的,这不容易“理解”。亲爱的Martin。。。去掉某些属性并修改结构是为了提高可见性。当然,不是在XML中,而是在一个布局软件中,其中添加了更多的特性,如彩色类型显示的前缀等。不幸的是,解析器仅适用于XSLt版本1。非常感谢您的努力,您的编码非常有启发性(虽然不是100%适用)——请注意——Chris
<p attr1="..." attr2="..." moreattr1="..." moreattr2="...">...content,more nodes...</p>