| | |
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:
Solved Threads: 0
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:
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:
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.
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)
<?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.
I found the following XSLT which works great if the text is already in an element:
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<?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.
•
•
Join Date: Apr 2009
Posts: 45
Reputation:
Solved Threads: 2
<?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
![]() |
Similar Threads
- XSLT attribute output as hyperlink (XML, XSLT and XPATH)
- XML reading (Python)
- Printing out a dictionary value (Python)
- Regular expressions (Python)
- Reading XML data (Python)
- Element 'html' cannot be nested within element 'div' (ASP.NET)
- Xpath (XML, XSLT and XPATH)
- Nee help with sending text to child windows (C#)
- Right Align Option Element Text (JavaScript / DHTML / AJAX)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: How to use following-sibling & starts-with function
- Next Thread: RSS problems
| Thread Tools | Search this Thread |
api blogger blogging code delete development dynamiccreationofnvariablesinxslt error firstthreecharacterofastringrequired flipbook gdata google html include java link linspire linux microsoft news node openoffice overwrite precedence programming rss standards swf template transform variable w3c web xml xmlnotloading xmlonserver xsl xslt





