Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Location
    Ohio
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    XSLT - Changing element text into a child element

    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:

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <newapplicantreturn>
    	<applicant id="1234567">
    		<questions>
    			<question order="1" id="123" answertype="YesNo">
    				This is question 1. 
    			</question>
    			<question order="2" id="124" answertype="YesNo">
    				This is question 2.
    			</question>
    			<question order="3" id="125" answertype="Text">
    				This is question 3.
    			</question>
    		</questions>
    	</applicant>
    </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.

    Example:

    Code:
    <question order="3" id="125" answertype="Text">
    	<text>This is question 3.</text>
    </question>
    I found the following XSLT which works great if the text is already in an element:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <xsl:stylesheet version="1.0"
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="@* | node()">
    	<xsl:copy>
    		<xsl:apply-templates select="@* | node()" />
    	</xsl:copy>
    </xsl:template>
    
    <xsl:template match="question">
    	<xsl:copy>
    		<xsl:apply-templates select="@*" />
    		<xsl:apply-templates select="*" mode="element-to-attribute"/>
    	</xsl:copy>
    </xsl:template>
    
    <xsl:template match="question/*" mode="element-to-attribute">
    	<xsl:attribute name="text">
    		<xsl:value-of select="." />
    	</xsl:attribute>
    </xsl:template>
    
    </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.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    try
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <xsl:stylesheet version="1.0"
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="@* | node()">
    	<xsl:copy>
    		<xsl:apply-templates select="@* | node()" />
    	</xsl:copy>
    </xsl:template>
    
    <xsl:template match="question">
    	<xsl:copy>
    		<xsl:apply-templates select="@*" />
    		<xsl:attribute name="text">
    			<xsl:value-of select="text()" />
    		</xsl:attribute>
    	</xsl:copy>
    </xsl:template>
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    reisende (05-15-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Location
    Ohio
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for taking the time to reply, shyam. I managed to find something that worked for me last night.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •