xslt 2.0 change node name and wrapping nodes from tokenize -



xslt 2.0 change node name and wrapping nodes from tokenize -

i began work on xslt. input , expected output follows , xslt given below.

i have next input xml:

<servicio> <control> <codser>00013</codser> <idmen>12378658936578</idmen> <coderr>000</coderr> <numid>000xxxxxxxx</numid> </control> <cuentas> <tippro>3</tippro> <numcta>000000006</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>001513003135</saldo> </cuentas> <cuentas> <tippro>1</tippro> <numcta>000000005</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>007573144537</saldo> <signo>pos</signo> </cuentas> <fondos> <tippro>4</tippro> <numcta>000000007</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>001513003135</saldo> </fondos> <fondos> <tippro>4</tippro> <numcta>000000008</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>007573144537</saldo> </fondos> </servicio>

and need apply xsl (output):

<servicio> <control> <codser>00013</codser> <idmen>12378658936578</idmen> <coderr>000</coderr> <numid>000xxxxxxxx</numid> </control> <cuentas> <cuenta> <tippro>3</tippro> <numcta>000000006</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>001513003135</saldo> </cuenta> <cuenta> <tippro>1</tippro> <numcta>000000005</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>007573144537</saldo> <signo>pos</signo> </cuenta> </cuentas> <fondos> <fondo> <tippro>4</tippro> <numcta>000000007</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>001513003135</saldo> </fondo> <fondo> <tippro>4</tippro> <numcta>000000008</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>007573144537</saldo> </fondo> </fondos> </servicio>

the xsl tried , didn't work is:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:output method="xml" /> <xsl:strip-space elements="*" /> <xsl:output indent="yes" /> <xsl:param name="elementinfo" select="cuentas,cuentas,cuenta;fondos,fondos,fondo"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*" /> </xsl:copy> </xsl:template> <xsl:template match="node()[name() = name(/*)]"> <xsl:variable name="inputnode" select="." /> <xsl:for-each select="tokenize($elementinfo, ';')"> <xsl:variable name="tokenizednodenames" select="tokenize(.,',')" /> <xsl:for-each select="$inputnode"> <xsl:call-template name="wrapping"> <xsl:with-param name="wrapperelementname" select="$tokenizednodenames[1]" /> <xsl:with-param name="oldelementname" select="$tokenizednodenames[2]" /> <xsl:with-param name="newelementname" select="$tokenizednodenames[3]" /> </xsl:call-template> </xsl:for-each> </xsl:for-each> </xsl:template> <xsl:template name="wrapping"> <xsl:param name="wrapperelementname" /> <xsl:param name="oldelementname" /> <xsl:param name="newelementname" /> <xsl:copy> <xsl:apply-templates select="node()[not(name() = $oldelementname)]" /> <xsl:element name="{$wrapperelementname}"> <xsl:for-each select="node()[name() = $oldelementname]"> <xsl:element name="{$newelementname}"> <xsl:copy-of select="@*|node()" /> </xsl:element> </xsl:for-each> </xsl:element> </xsl:copy> </xsl:template>

when have input xml whit cuentas works when tried whit fondos geting mal format xml.

i think want grouping elements cuentas , fondos node-name():

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="#all" version="2.0"> <xsl:strip-space elements="*"/> <xsl:output indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* , node()"/> </xsl:copy> </xsl:template> <xsl:template match="/*"> <xsl:copy> <xsl:for-each-group select="*" group-by="node-name(.)"> <xsl:apply-templates select="." mode="wrap"/> </xsl:for-each-group> </xsl:copy> </xsl:template> <xsl:template match="*" mode="wrap"> <xsl:apply-templates select="current-group()"/> </xsl:template> <xsl:template match="cuentas | fondos" mode="wrap"> <xsl:copy> <xsl:apply-templates select="current-group()"/> </xsl:copy> </xsl:template> <xsl:template match="cuentas | fondos"> <xsl:element name="{substring(local-name(), 1, string-length(local-name()) - 1)}"> <xsl:apply-templates select="@* | node()"/> </xsl:element> </xsl:template> </xsl:stylesheet>

that way, saxon 9.5, result

<servicio> <control> <codser>00013</codser> <idmen>12378658936578</idmen> <coderr>000</coderr> <numid>000xxxxxxxx</numid> </control> <cuentas> <cuenta> <tippro>3</tippro> <numcta>000000006</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>001513003135</saldo> </cuenta> <cuenta> <tippro>1</tippro> <numcta>000000005</numcta> <tipcta>a</tipcta> <descta>cuenta i</descta> <saldo>007573144537</saldo> <signo>pos</signo> </cuenta> </cuentas> <fondos> <fondo> <tippro>4</tippro> <numcta>000000007</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>001513003135</saldo> </fondo> <fondo> <tippro>4</tippro> <numcta>000000008</numcta> <tipcta>a</tipcta> <descta>fondo i</descta> <saldo>007573144537</saldo> </fondo> </fondos> </servicio>

xslt-2.0 tokenize wrap

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -