Xml XSLT-剪切和粘贴
如何在xslt中进行剪切和粘贴?假设我的xml是这样的Xml XSLT-剪切和粘贴,xml,xslt,xpath,Xml,Xslt,Xpath,如何在xslt中进行剪切和粘贴?假设我的xml是这样的 <root> <header> some nodes </header> <body> <p> some text <link>1</link> <note xml:id="c5-note-0001" numbered="no">text</note>
<root>
<header>
some nodes
</header>
<body>
<p>
some text
<link>1</link>
<note xml:id="c5-note-0001" numbered="no">text</note>
</p>
<p>
some text
<link>2</link>
<figure>
<note xml:id="c5-note-0003">text</note>
</figure>
<note xml:id="c5-note-0002">text</note>
</p>
<tabular>
<note xml:id="c5-note-0004" numbered="no">text</note>
</tabular>
<p>
some text
<link>3</link>
<notegroup>
<note xml:id="c5-note-0006">text</note>
</notegroup>
<note xml:id="c5-note-0005">text</note>
</p>
<p>
some text
<link>4</link>
<note xml:id="c5-note-0007">text</note>
</p>
</body>
</root>
提前感谢。一般来说,在XSLT中,将“剪切和粘贴”想象成实际情况是有益的,即“复制和粘贴并删除原始内容”
我需要在body标记的末尾之前创建一个新节点newnote,然后将note节点剪切并粘贴到该节点中
您要做的是调整
元素,使其在原始内容(可能被修改)之后包含一个新的
元素。因此,为
元素添加一个新模板:
<xsl:template match="body">
</xsl:template>
如前所述,您希望基本上接管原始内容。由于原始内容可能仍由其他模板处理,我们将在此处使用
:
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
在原始内容之后,要插入新元素:
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<newNote>
</newNote>
</xsl:template>
最后,在此元素中,您需要所描述的所有
元素的副本,这可以通过
循环实现:
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<newnote>
<xsl:for-each select="p/note[not(@numbered = 'no')]">
<xsl:copy-of select="."/>
</xsl:for-each>
</newnote>
</xsl:template>
并且需要生成一个notenum节点,而不是该注释
这可以通过模板替换相应的
元素来完成:
<xsl:template match="p/note[not(@numbered = 'no')]">
<notenum><xsl:value-of select="count(preceding::note[parent::p][not(@numbered = 'no')]) + 1"/></notenum>
</xsl:template>
插入新的
元素,然后使用
命令输出计算值。该值是符合您的限制条件的前面的
元素的数量加上一个。我在您的预期输出中没有看到任何
元素,即使它据称是(根据您的描述文本)生成的。另外,您是指剪切粘贴还是复制粘贴有关
元素的内容?计数节点表示生成的notenum
,而不是note
。我需要剪贴。请看我的输入和预期输出;对移动或不移动的各种
元素感到困惑。我添加了一个合适的答案,希望能满足您的需要。
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<newnote>
<xsl:for-each select="p/note[not(@numbered = 'no')]">
<xsl:copy-of select="."/>
</xsl:for-each>
</newnote>
</xsl:template>
<xsl:template match="p/note[not(@numbered = 'no')]">
<notenum><xsl:value-of select="count(preceding::note[parent::p][not(@numbered = 'no')]) + 1"/></notenum>
</xsl:template>