Solved

XSLT: Transforming a leaf node

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

690 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