Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XSLT: Transforming a leaf node

Posted on 2010-09-23
5
Medium Priority
?
968 Views
Last Modified: 2013-11-18
Hi,
I'm new on XSL an XPATH, I'm trying to create an xsl tranformation to do that:

XML to transform

<test id="a">
<leaf attribute="1">some text</leaf>
</test>

Result Wanted:
<test id="a">
<leaf>
<attribute>1</attribute>
<leaf> some text</leaf>
</leaf>
</test>

The XSL transformation:

If a tag (element) has an attribute, and text value:
Example: <leaf attribute="1">some text</leaf>

convert the attribute to an element, and add a child node with the same name as the element:
<leaf>
<attribute>1</attribute> // attribute converted to an element
<leaf> some text</leaf> // a new sub-element with the same name as the containing element
</leaf>

But if a tag with an attribute has no text-element:
Example: the "test" element
<test id="a">
<leaf>
...
</leaf>
</test>

Do nothing.
0
Comment
Question by:jaypi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33749454
This should get you started
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="*[string-length(normalize-space(text())) > 0]">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:element name="{name()}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
            <xsl:element name="{name()}">
                <xsl:apply-templates select="node()"/>
            </xsl:element>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[string-length(normalize-space(text())) = 0]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:jaypi
ID: 33751596
Thanks Gertone, but this is not the result expect:

Input XML:

<test>blabla</test>

Result:

<test><test>blabla</test></test>

Result expected:

<test>blabla</test>

Here the "test" tag doesn't have any attribute, so It should remain the same.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 33751666
ok, minor fix
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="*[string-length(normalize-space(text())) > 0][@*]">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:element name="{name()}">
                    <xsl:value-of select="."/>
                </xsl:element>
            </xsl:for-each>
            <xsl:element name="{name()}">
                <xsl:apply-templates select="node()"/>
            </xsl:element>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:jaypi
ID: 33751701
Thanks Flash Gordon/Gertone for your very quick and excellent answer :D
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 33751708
welcome
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question