this might be impossible, but u guys might have an answer,
im tryin to split this xml
<CTP>
<name>ABSA bank</name>
<BAs.BA>bank|sector|issuer</BAs.BA>
</CTP>
and transform it to this form :
<CTP>
<name>ABSA bank</name>
<BAs>
<BA>bank</BA>
<BA>sector</BA>
<BA>issuer</BA>
</BAs>
</CTP>
i could do this using this code
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" version="1.0" extension-element-prefixes="str">
<xsl:template name="str:tokenize">
<xsl:param name="string" select="''" />
<xsl:param name="delimiters" select="'	
'" />
<xsl:choose>
<xsl:when test="not($string) or (string-length($string)=0)" />
<xsl:when test="not($delimiters) or (string-length($delimiters)=0)">
<xsl:call-template name="str:_tokenize-characters">
<xsl:with-param name="string" select="$string" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str:_tokenize-delimiters">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="delimiters" select="$delimiters" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="str:_tokenize-characters">
<xsl:param name="string" />
<xsl:if test="$string">
<BA>
<xsl:value-of select="substring($string, 1, 1)" />
</BA>
<xsl:call-template name="str:_tokenize-characters">
<xsl:with-param name="string" select="substring($string, 2)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="str:_tokenize-delimiters">
<xsl:param name="string" />
<xsl:param name="delimiters" />
<xsl:variable name="delimiter" select="substring($delimiters, 1, 1)" />
<xsl:choose>
<xsl:when test="string-length($delimiter)=0">
<BA>
<xsl:value-of select="$string" />
</BA>
</xsl:when>
<xsl:when test="contains($string, $delimiter)">
<xsl:choose>
<xsl:when test="not(starts-with($string, $delimiter))">
<xsl:call-template name="str:_tokenize-delimiters">
<xsl:with-param name="string" select="substring-before($string, $delimiter)" />
<xsl:with-param name="delimiters" select="substring($delimiters, 2)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<BA />
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="str:_tokenize-delimiters">
<xsl:with-param name="string" select="substring-after($string, $delimiter)" />
<xsl:with-param name="delimiters" select="$delimiters" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str:_tokenize-delimiters">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="delimiters" select="substring($delimiters, 2)" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="v1">
<xsl:value-of select="CTP/BAs.BA" />
</xsl:variable>
<BAs>
<xsl:call-template name="str:tokenize">
<xsl:with-param name="string" select="$v1" />
<xsl:with-param name="delimiters" select="'#'" />
</xsl:call-template>
</BAs>
</xsl:template>
</xsl:stylesheet>
but the main challenge is to make it dynamic, in other words i want the 'BAs'
and 'BA' Nodes to be dynamically written in case i got other XML like:
<CTP>
<name>ABSA bank</name>
<FAs.FA>dep|sec|issue</BAs.BA>
</CTP>
i want it to look like this:
<CTP>
<name>ABSA bank</name>
<FAs>
<FA>bank</BA>
<FA>sector</BA>
<FA>issuer</BA>
</FAs>
</CTP>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…