Move elements from one place to another using XSLT 2 -


move elements 1 place using xslt 2 (saxon8)

hi please me produce following output using xslt 2.0.

i have this:

<ul>  <item>     aaa      <nl>iii         <item1>111</item1>         <item2>222</item2>     </nl>  </item>  <item>     bbb     <nl>vvv         <item1>333</item1>         <item2>444</item2>     </nl>  </item>  </ul> 

i need produce this

<ul>  <item>     aaa     <item1>111</item1>  </item>  <item>     bbb     <item1>333</item1>  </item>  <new>   <nl>iii    <item2>222</item2>   </nl>    <nl>vvv    <item2>444</item2>   </nl>  </new> </ul> 

i trying output using mode option mentioned below:

<xsl:stylesheet version="2.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="nl"/>     <xsl:template match="item2"/>      <xsl:template match="ul">       <xsl:copy>     <xsl:apply-templates/>     <new>       <xsl:apply-templates select="descendant::nl" mode="move"/>     </new>       </xsl:copy>     </xsl:template>      <xsl:template match="item" mode="move">         <xsl:copy>             <xsl:apply-templates/>         <xsl:apply-templates select="descendant::item2" mode="move1"/>         </xsl:copy>     </xsl:template>      <xsl:template match="nl" mode="move">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>       <xsl:template match="item2" mode="move1">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template>  </xsl:stylesheet>  

thanks , regards

bala

here xslt 2.0 stylesheet creates structure want, although white space different, problem mixed content produce exact output:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:xs="http://www.w3.org/2001/xmlschema"     exclude-result-prefixes="xs"     version="2.0">      <xsl:template match="ul">         <xsl:copy>             <xsl:for-each-group select="item" group-by="nl/*/node-name(.)">                 <xsl:choose>                     <xsl:when test="position() eq 1">                         <xsl:for-each select="current-group()">                             <item>                                 <xsl:apply-templates select="text() | nl/*[node-name(.) eq current-grouping-key()]"/>                             </item>                         </xsl:for-each>                     </xsl:when>                     <xsl:otherwise>                         <new>                             <xsl:for-each select="current-group()">                                 <nl>                                     <xsl:apply-templates select="nl/(text() | *[node-name(.) eq current-grouping-key()])"/>                                 </nl>                             </xsl:for-each>                         </new>                     </xsl:otherwise>                 </xsl:choose>             </xsl:for-each-group>         </xsl:copy>     </xsl:template>      <xsl:template match="nl/*">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

i result

<ul><item>         aaa          <item1>111</item1>     </item><item>         bbb         <item1>333</item1>     </item><new><nl>iii              <item2>222</item2>         </nl><nl>vvv              <item2>444</item2>         </nl></new></ul> 

when add instructions strip white space input , indent output, in

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:xs="http://www.w3.org/2001/xmlschema"     exclude-result-prefixes="xs"     version="2.0">      <xsl:strip-space elements="*"/>     <xsl:output indent="yes"/>      <xsl:template match="ul">         <xsl:copy>             <xsl:for-each-group select="item" group-by="nl/*/node-name(.)">                 <xsl:choose>                     <xsl:when test="position() eq 1">                         <xsl:for-each select="current-group()">                             <item>                                 <xsl:apply-templates select="text() | nl/*[node-name(.) eq current-grouping-key()]"/>                             </item>                         </xsl:for-each>                     </xsl:when>                     <xsl:otherwise>                         <new>                             <xsl:for-each select="current-group()">                                 <nl>                                     <xsl:apply-templates select="nl/(text() | *[node-name(.) eq current-grouping-key()])"/>                                 </nl>                             </xsl:for-each>                         </new>                     </xsl:otherwise>                 </xsl:choose>             </xsl:for-each-group>         </xsl:copy>     </xsl:template>      <xsl:template match="nl/*">         <xsl:copy>             <xsl:apply-templates/>         </xsl:copy>     </xsl:template> </xsl:stylesheet> 

the result is

<ul>    <item>         aaa          <item1>111</item1>    </item>    <item>         bbb         <item1>333</item1>    </item>    <new>       <nl>iii             <item2>222</item2>       </nl>       <nl>vvv             <item2>444</item2>       </nl>    </new> </ul> 

as alternative, if know there item1 , item2 want split, here stylesheet using modes:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:output indent="yes"/>     <xsl:strip-space elements="*"/>      <xsl:template match="@*|node()" mode="#all">         <xsl:copy>             <xsl:apply-templates select="@*|node()" mode="#current"/>         </xsl:copy>     </xsl:template>      <xsl:template match="nl">         <xsl:apply-templates select="item1"/>     </xsl:template>      <xsl:template match="item2"/>      <xsl:template match="ul">         <xsl:copy>             <xsl:apply-templates/>             <new>                 <xsl:apply-templates select="descendant::nl" mode="move"/>             </new>         </xsl:copy>     </xsl:template>      <xsl:template match="item1" mode="move"/>  </xsl:stylesheet>  

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -