p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   XSLT (http://p2p.wrox.com/xslt-86/)
-   -   element text to element name xslt (http://p2p.wrox.com/xslt/99906-element-text-element-name-xslt.html)

smok.hcl@gmail.com June 7th, 2018 11:18 PM

element text to element name xslt
 
I want to convert element text to element name. I may sound wierd. let me give an example

<root>
<repeat>
<a1>header1</a1>
<a2>header2</a2>
<a3>header3</a3>
</repeat>
<repeat>
<a1>value11</a1>
<a2>value12</a2>
<a3>value13</a3>
</repeat>
<repeat>
<a1>value21</a1>
<a2>value22</a2>
<a3>value23</a3>
</repeat>
</root>


expected output

<root>
<repeat>
<header1>value11</header1>
<header2>value12</header2>
<header3>value13</header3>
</repeat>
<repeat>
<header1>value21</header1>
<header2>value22</header2>
<header3>value23</header3>
</repeat>
</root>

Please help.

mhkay June 8th, 2018 03:50 AM

Despite the title of your question, you don't seem to be changing the "element text" at all, you are only changing the name.

Define a mapping from old names to new names like this:

Code:

<xsl:key name="renamings" match="root/repeat[1]/*" use="local-name()"/>
And then convert elements like this:

Code:

<xsl:template match="root/repeat[position() > 1]/*">
  <xsl:element name="{key('renamings', local-name())}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

And of course you need templates to copy the bits you want to copy and delete the bits you want to delete, but that's all perfectly standard stuff.


All times are GMT -4. The time now is 02:32 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.