neobackup08
neobackup08

Reputation: 15

XSL to get exactly the same html code as in the XML element

XML/XSL newbie here, so please forgive my ignorance. This is the xml input given to me:

<entry>
    <desc>
        <p>Lorem ipsum <a href="http://www.google.com">dolor</a> sit amet.</p>
        <p>Lorem <strong>ipsum</strong> dolor sit amet.</p>
    </desc>
</entry>

I must write an XSL to get this result:

<p>Lorem ipsum <a href="http://www.google.com">dolor</a> sit amet.</p>
<p>Lorem <strong>ipsum</strong> dolor sit amet.</p>

I thought this would work, in an entry template:

<xsl:value-of select="desc"/>

but instead this is the output of the above line:

Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.

Am I correct in assuming there must be other xsl stylesheets on my system that do that? In any case, I have neither read not edit access to them, I can only add a new stylesheet, so to fix this I used this:

<xsl:for-each select="desc/*">
    <p><xsl:value-of select="."/></p>
</xsl:for-each>

This gets me, at least:

<p>Lorem ipsum dolor sit amet.</p><p>Lorem ipsum dolor sit amet.</p>

However as you can see links and formatting have gone bye bye! I have no control on the kind of html elements that get added in there. Can anybody suggest a way to preserving the formatting and all of the desc element? The engine is XSL 1 only. Thanks in advance!

Upvotes: 1

Views: 187

Answers (3)

user140327
user140327

Reputation:

Your select is incorrect.

select="desc"

or

select="/entry/desc"

From http://w3schools.com/xpath/xpath_syntax.asp:

nodename Selects all child nodes of the named node

/ Selects from the root node

// Selects nodes in the document from the current node that match the selection no matter where they are

. Selects the current node

.. Selects the parent of the current node

@ Selects attributes

Upvotes: 0

Andrew Hare
Andrew Hare

Reputation: 351446

Try this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/entry/desc">
        <xsl:copy-of select="@*|node()"/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Welbog
Welbog

Reputation: 60378

You need to copy the elements under desc and all of their sub-elements. The copy-of XSL element will do this for you:

<xsl:copy-of select="desc/node()"/>

That will copy all of the children of desc, including text nodes, and any of their sub-elements.

Upvotes: 4

Related Questions