XSLT - Changing element text into a child element

Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 3
Reputation: reisende is an unknown quantity at this point 
Solved Threads: 0
reisende reisende is offline Offline
Newbie Poster

XSLT - Changing element text into a child element

 
0
  #1
May 13th, 2009
Hi everyone,

I've been combing the message boards for the last few days trying to find something that will work for me, but to no success. I had 0 experience with XSLT before this which definitely doesn't help out in the search but I am determined to learn.

What I've got is some XML:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <newapplicantreturn>
  3. <applicant id="1234567">
  4. <questions>
  5. <question order="1" id="123" answertype="YesNo">
  6. This is question 1.
  7. </question>
  8. <question order="2" id="124" answertype="YesNo">
  9. This is question 2.
  10. </question>
  11. <question order="3" id="125" answertype="Text">
  12. This is question 3.
  13. </question>
  14. </questions>
  15. </applicant>
  16. </newapplicantreturn>

What I need to do is take the text in the question element and either add it as an attribute of the question or wrap it in a separate <text></text> child element. I'd prefer the second method but at this point, whatever works.

I found the following XSLT which works great if the text is already in an element:

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <xsl:stylesheet version="1.0"
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5.  
  6. <xsl:template match="@* | node()">
  7. <xsl:copy>
  8. <xsl:apply-templates select="@* | node()" />
  9. </xsl:copy>
  10. </xsl:template>
  11.  
  12. <xsl:template match="question">
  13. <xsl:copy>
  14. <xsl:apply-templates select="@*" />
  15. <xsl:apply-templates select="*" mode="element-to-attribute"/>
  16. </xsl:copy>
  17. </xsl:template>
  18.  
  19. <xsl:template match="question/*" mode="element-to-attribute">
  20. <xsl:attribute name="text">
  21. <xsl:value-of select="." />
  22. </xsl:attribute>
  23. </xsl:template>
  24.  
  25. </xsl:stylesheet>

I don't know if the above is easily tweakable to get what I want. I've been researching and trying so many different things I've pretty much hit the wall.

Any help will be greatly appreciated.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 46
Reputation: xml_looser is an unknown quantity at this point 
Solved Threads: 2
xml_looser xml_looser is offline Offline
Light Poster

Re: XSLT - Changing element text into a child element

 
0
  #2
May 14th, 2009
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output indent="yes" method="xml"/>
	<xsl:template match="newapplicantreturn">
		<xsl:variable name="wert1" select="local-name()"/>
		<xsl:element name="{$wert1}">
			<xsl:apply-templates select="applicant"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="applicant">
		<xsl:copy-of select="@*"/>
		<xsl:apply-templates select="questions"/>
	</xsl:template>
	<xsl:template match="questions">
		<questions>
			<xsl:apply-templates select="question"/>
		</questions>
	</xsl:template>
	<xsl:template match="question">
		<question>
			<xsl:attribute name="order">
				<xsl:value-of select="@order"/>
			</xsl:attribute>
			<xsl:attribute name="id">
				<xsl:value-of select="@id"/>
			</xsl:attribute>
			<xsl:attribute name="answertype">
				<xsl:value-of select="@answertype"/>
			</xsl:attribute>
			<!-- node value to attribut -->
			<xsl:attribute name="text">
				<xsl:value-of select="."/>
			</xsl:attribute>
			<!-- node value to child element -->
			<text><xsl:value-of select="."/></text>
		</question>
	</xsl:template>
</xsl:stylesheet>


Helmut Hagemann
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: reisende is an unknown quantity at this point 
Solved Threads: 0
reisende reisende is offline Offline
Newbie Poster

Re: XSLT - Changing element text into a child element

 
0
  #3
May 14th, 2009
Looks promising, Helmut. Thanks for the reply. I'll give that a try and reply back with results.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: reisende is an unknown quantity at this point 
Solved Threads: 0
reisende reisende is offline Offline
Newbie Poster

Re: XSLT - Changing element text into a child element

 
0
  #4
May 14th, 2009
Thanks again, Helmut! That was just what I was looking for.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the XML, XSLT and XPATH Forum


Views: 981 | Replies: 3
Thread Tools Search this Thread



Tag cloud for XML, XSLT and XPATH
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC