Solved

XSLT: Transforming a leaf node

Posted on 2010-09-23
5
943 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
  • 3
  • 2
5 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Thanks Flash Gordon/Gertone for your very quick and excellent answer :D
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now