xml - Split Strings into dynamic element names and values -


i have string 1 of nodes in xml going return delimited list of values. similar string:

<item>code^returncode|description^returndescription|system^returnsystem</item> 

what looking output of

<item>   <code>returncode</code>    <description>returndescription</description>    <system>returnsystem</system> <item> 

the element names , values dynamic. know getting close idea, first xslt. here have far:

<xsl:template match="/container/information" >    <xsl:call-template name="splitreturn">         <xsl:with-param name="string" select="text()"/>         <xsl:with-param name="separator" select="'|'"/>    </xsl:call-template> </xsl:template>  <xsl:template name="splitreturn">     <xsl:param name="string"/>     <xsl:param name="separator"/>     <xsl:element name="{substring-before($string,$separator)}">         <xsl:value-of select="substring-after($string,$separator)" />     </xsl:element>   </xsl:template> 

i think missing how nest templates applying.

you must make template recursive. try:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform" > <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="/item">     <xsl:copy>         <xsl:call-template name="splitreturn"/>     </xsl:copy> </xsl:template>       <xsl:template name="splitreturn">     <xsl:param name="string" select="."/>     <xsl:variable name="token" select="substring-before(concat($string, '|'), '|')" />         <xsl:if test="$token">         <xsl:element name="{substring-before($token, '^')}">             <xsl:value-of select="substring-after($token, '^')" />         </xsl:element>       </xsl:if>     <xsl:if test="contains($string, '|')">         <!-- recursive call -->         <xsl:call-template name="splitreturn">             <xsl:with-param name="string" select="substring-after($string, '|')"/>         </xsl:call-template>     </xsl:if> </xsl:template>  </xsl:stylesheet> 

when applied input example:

<item>code^returncode|description^returndescription|system^returnsystem</item> 

the result be:

<?xml version="1.0" encoding="utf-8"?> <item>    <code>returncode</code>    <description>returndescription</description>    <system>returnsystem</system> </item> 

note work if provided names valid xml element names.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -