Solved

XSLT: Transforming a leaf node

Posted on 2010-09-23
5
946 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
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

867 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

20 Experts available now in Live!

Get 1:1 Help Now