how to do cut and paste in xslt? suppose my xml like this,
<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>
my expected output is,
<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>
<notenum>1</notenum>
</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>
<notenum>2</notenum>
</p>
<p>
some text
<link>4</link>
<notenum>3</notenum>
</p>
<newnote>
<note xml:id="c5-note-0002">text</note>
<note xml:id="c5-note-0005">text</note>
<note xml:id="c5-note-0007">text</note>
</newnote>
</body>
</root>
i need to create a new node newnote before the end of body tag and cut and paste the note node into that and need to generate a notenum node instead of that note.
i need to do this only within the p node. if the note comes under tabular, figure and notegroup then no need to do anything.
if note contains attribute like numbered="no" then no need to do anything.
i am using the following xslt(just to show the template match that i am using),
<?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:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match ="figure">
some operation
</xsl:template>
<xsl:template match ="tabular">
some operation
</xsl:template>
<xsl:template match ="notegroup">
some operation
</xsl:template>
</xsl:stylesheet>
thanks in advance.
In general, it’s beneficial in XSLT to imagine “cut and paste” as what it actually is, namely “copy and paste and delete the original”.
What you want to do is adapt the
<body>element so it contains, after its (possibly otherwise modified) original contents, a new<newnote>element. Therefore, add a new template for the<body>element:As mentioned before, you want to basically take over the original contents. As that original contents may still be processed by other templates, we’ll use
<xsl:apply-tepmlates>here:After the original contents, you want to insert the new element:
Finally, in this element, you want a copy of all the described
<note>elements, which can be achieved with an<xsl:for-each>loop:This can be done with a template replacing the respective
<note>elements:Insert the new
<notenum>element instead and use the<xsl:value-of>command to output a computed value. The value is the number of preceding<note>elements that match your restrictions plus one.