xml - How do I create an xsl template that can write the element's name and value as a KVP then process all sibling and child nodes recursively -


so need create transform can return name of element , value in kvp format tab delimeter. trick is, need node , child nodes returned in teh list. ordering not important

xml sample:

<holding id="holding_1">   <holdingtypecode tc="2">policytype</holdingtypecode>   <holdingstatus tc="3">proposedstatus</holdingstatus>   <policy id="policy_1">     <polnumber>0123456789</polnumber>     <lineofbusiness tc="1">lifebusiness</lineofbusiness>   </policy> </holding> 

in xslt have call template:

<xsl:call-template name="list_siblings_and_children_recursive">   <xsl:with-param name="parent" select="//holding"></xsl:with-param> </xsl:call-template> 

and template itself:

<xsl:template name="list_siblings_and_children_recursive"> <xsl:param name="parent"></xsl:param>  <!-- tab & crlf char--> <xsl:variable name="tab">   <xsl:text>&#x9;</xsl:text> </xsl:variable> <xsl:variable name="crlf">   <xsl:text>&#10;&#13;</xsl:text> </xsl:variable>  <!-- list each element in list of siblings, recursively check children --> <xsl:for-each select="$parent/*">   <xsl:value-of select="local-name()"/>   <xsl:value-of select="$tab"/>   <xsl:value-of select="."/>   <xsl:value-of select="$crlf"/>   <xsl:call-template name="list_siblings_and_children_recursive">     <xsl:with-param name="parent" select="./*"></xsl:with-param>   </xsl:call-template> </xsl:for-each> 

what wrong template? output i'm looking should come out like:

holdingtypecode    policytype holdingstatus      proposedstatus policy polnumber     0123456789 lineofbusiness     lifebusiness 

what wrong template?

afaict, main problem recursive call:

<xsl:call-template name="list_siblings_and_children_recursive">   <xsl:with-param name="parent" select="./*"></xsl:with-param> </xsl:call-template> 

i believe should be:

<xsl:call-template name="list_siblings_and_children_recursive">   <xsl:with-param name="parent" select="."></xsl:with-param> </xsl:call-template> 

note xslt's native processing model recursive, make lot simpler taking advantage of - like:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text" encoding="utf-8"/> <xsl:strip-space elements="*"/>  <xsl:template match="*">     <xsl:value-of select="name()"/>     <xsl:text>&#x9;</xsl:text>     <xsl:value-of select="text()"/>     <xsl:text>&#10;&#13;</xsl:text>     <xsl:apply-templates select="*"/> </xsl:template>  </xsl:stylesheet> 

note difference between:

<xsl:value-of select="."/> 

and:

<xsl:value-of select="text()"/> 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -