Solved

XSLT: Transforming a leaf node

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json format text only 4 86
XML Data Missing in PHP SimpleXML 8 64
Fixing Non-Standard characters in text 8 104
Automating receiving uploaded files via HTTPS instead of SFTP 3 44
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

832 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