Jump to content

What is XSLT for below XML


sadiqmodan

Recommended Posts

I have XML file in below format.

<Answers>  <AnswerSet ID="1">		<Answer questionId="ItemName">qwe</Answer>	<Answer questionId="Options" type="list" selectedValue="2">Update</Answer>	<Answer questionId="NodeType" type="list" selectedValue="8">Test</Answer>	<Answer questionId="ItemNodeName">Hello Testing111111</Answer>	<Answer questionId="ConnectionString">Testing</Answer>	<Answer questionId="Sql">select * from table123eeee</Answer>	<Answer questionId="Question" type="list" selectedValue="1" RepeatRegion="0">clientID</Answer>	<Answer questionId="Column" RepeatRegion="0">ClientID</Answer>	<Answer questionId="Question" type="list" selectedValue="4" RepeatRegion="1">age</Answer>	<Answer questionId="Column" RepeatRegion="1">Age</Answer>	<Answer questionId="Question" type="list" selectedValue="2" RepeatRegion="2">name</Answer>	<Answer questionId="Column" RepeatRegion="2">Name</Answer>	<Answer questionId="Question" type="list" selectedValue="3" RepeatRegion="3">DOB</Answer>	<Answer questionId="Column" RepeatRegion="3">DOB</Answer>  </AnswerSet></Answers>

I need format like below

<ImportList>  <ImportItem>	<ItemName>qwe</ItemName>	<Options>Update</Options>	<NodeType>Test</NodeType>	<ItemNodeName>Hello Testing111111</ItemNodeName>	<ConnectionString>Testing</ConnectionString>	<Sql>select * from table123eeee</Sql>	<QuestionItem>		<Question>clientID</Question>		<Column>ClientID</Column>	</QuestionItem>	<QuestionItem>		<Question>age</Question>		<Column>Age</Column>	</QuestionItem>	<QuestionItem>		<Question>name</Question>		<Column>Name</Column>	</QuestionItem>	<QuestionItem>		<Question>DOB</Question>		<Column>DOB</Column>	</QuestionItem>  </ImportItem></ImportList>

I have Implemented XSLT which is 2.0 but it will not return proper result.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="Answers">	<ImportList>	  <xsl:apply-templates select="AnswerSet"/>	</ImportList>  </xsl:template>  <xsl:template match="AnswerSet">	<ImportItem>	  <xsl:for-each select="Answer">		<xsl:variable name="questionId" select="@questionId"/>		<xsl:if test="not(@RepeatRegion)">		  <xsl:element name="{$questionId}">			<xsl:value-of select="."/>		  </xsl:element>		</xsl:if>	  </xsl:for-each>	  <xsl:for-each select="Answer">		<xsl:variable name="questionId" select="@questionId"/>		<xsl:if test="@RepeatRegion and not(@RepeatRegion = preceding-sibling::Answer/@RepeatRegion)">		  <QuestionItem></QuestionItem>		 </xsl:if>		<xsl:if test="@RepeatRegion">		  <xsl:element name="{$questionId}">			<xsl:value-of select="."/>		  </xsl:element>		</xsl:if>			  </xsl:for-each>	</ImportItem>  </xsl:template></xsl:stylesheet>

Please help me out..Thanks in advanceRegards,Sadiq

Link to comment
Share on other sites

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0"  xmlns:xs="http://www.w3.org/2001/XMLSchema"  exclude-result-prefixes="xs">    <xsl:output indent="yes"/>  <xsl:strip-space elements="*"/>    <xsl:template match="Answers">	<ImportList>	  <xsl:apply-templates/>	</ImportList>  </xsl:template>    <xsl:template match="AnswerSet">	<ImportItem>	  <xsl:apply-templates select="Answer[not(@questionId = 'Column')]"/>	</ImportItem>  </xsl:template>    <xsl:template match="Answer[not(@questionId = ('Question', 'Column'))]">	<xsl:element name="{@questionId}">	  <xsl:value-of select="."/>	</xsl:element>  </xsl:template>    <xsl:template match="Answer[@questionId = 'Question']">	<QuestionItem>	  <Question>		<xsl:value-of select="."/>	  </Question>	  <Column>		<xsl:value-of select="following-sibling::Answer[@questionId = 'Column'][1]"/>	  </Column>	</QuestionItem>  </xsl:template>  </xsl:stylesheet>

should do the job, at least for the input sample and the output you asked for. I am not sure however I have fully understood and implemented your requirements.

Link to comment
Share on other sites

Hi Martin Honnen,Thanks for your quick response.we can not check with ID attribute value because it may be different means value of ID is creating dynamically. We can check value of RepeatRegion attribute, in sample we have RepeatRegion value two times like

<Answer questionId="Question" type="list" selectedValue="1" RepeatRegion="0">clientID</Answer><Answer questionId="Column" RepeatRegion="0">ClientID</Answer>

so the transform xml look like

 <QuestionItem>	  <Question>clientID</Question>	  <Column>ClientID</Column> </QuestionItem>

RepeatRegion may be vary like it is coming 3 time, 1 time dependent on sample but transform structure is fix. So using RepeatRegion value we can create <QuestionItem> tag.Hope you understand the my question properly... can you please provide better solution...Thanks in advance.Regards.Sadiq.

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0"  xmlns:xs="http://www.w3.org/2001/XMLSchema"  exclude-result-prefixes="xs">    <xsl:output indent="yes"/>  <xsl:strip-space elements="*"/>    <xsl:template match="Answers">	<ImportList>	  <xsl:apply-templates/>	</ImportList>  </xsl:template>    <xsl:template match="AnswerSet">	<ImportItem>	  <xsl:apply-templates select="Answer[not(@questionId = 'Column')]"/>	</ImportItem>  </xsl:template>    <xsl:template match="Answer[not(@questionId = ('Question', 'Column'))]">	<xsl:element name="{@questionId}">	  <xsl:value-of select="."/>	</xsl:element>  </xsl:template>    <xsl:template match="Answer[@questionId = 'Question']">	<QuestionItem>	  <Question>		<xsl:value-of select="."/>	  </Question>	  <Column>		<xsl:value-of select="following-sibling::Answer[@questionId = 'Column'][1]"/>	  </Column>	</QuestionItem>  </xsl:template>  </xsl:stylesheet>

should do the job, at least for the input sample and the output you asked for. I am not sure however I have fully understood and implemented your requirements.

Link to comment
Share on other sites

Hi Martin Honnen,Thanks for your quick response.we can not check with ID attribute value because it may be different means value of ID is creating dynamically. We can check value of RepeatRegion attribute, in sample we have RepeatRegion value two times like
<Answer questionId="Question" type="list" selectedValue="1" RepeatRegion="0">clientID</Answer><Answer questionId="Column" RepeatRegion="0">ClientID</Answer>

so the transform xml look like

 <QuestionItem>	  <Question>clientID</Question>	  <Column>ClientID</Column> </QuestionItem>

RepeatRegion may be vary like it is coming 3 time, 1 time dependent on sample but transform structure is fix. So using RepeatRegion value we can create <QuestionItem> tag.Hope you understand the my question properly... can you please provide better solution...Thanks in advance.Regards.Sadiq.

Link to comment
Share on other sites

I am not sure I understand your requirements. The following XSLT 2.0 groups based on the attribute "RepeatRegion":

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">    <xsl:output indent="yes"/>    <xsl:template match="Answers">	<ImportLists>	  <xsl:apply-templates/>	</ImportLists>  </xsl:template>    <xsl:template match="AnswerSet">	<ImportItem>	  <xsl:for-each-group select="Answer" group-adjacent="boolean(@RepeatRegion)">		<xsl:choose>		  <xsl:when test="current-grouping-key()">			<xsl:for-each-group select="current-group()" group-adjacent="@RepeatRegion">			  <QuestionItem>				<xsl:apply-templates select="current-group()"/>			  </QuestionItem>			</xsl:for-each-group>		  </xsl:when>		  <xsl:otherwise>			<xsl:apply-templates select="current-group()"/>		  </xsl:otherwise>		</xsl:choose>	  </xsl:for-each-group>	</ImportItem>  </xsl:template>    <xsl:template match="Answer">	<xsl:element name="{@questionId}">	  <xsl:value-of select="."/>	</xsl:element>  </xsl:template>  </xsl:stylesheet>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...