xslt unwrap only the first para element in an entry element

Viewed 25

Some entry elements in a Dita table are formatted incorrectly:

<entry><p>Description</p></entry>
<entry><p>Description2</p><p>Description3</p><p>Description4</p>

If there is only one <p> element in a <entry> element I want to unwrap/remove the <p> element, just leaving the text. If there are more than 1 <p> elements in an <entry> element, I want to upwrap/remove just the first one. I want to leave all the other elements in the file the same.

So the output would look like:

<entry>Description</entry>
<entry>Description2<p>Description3</p><p>Description4</p>

I know this is easy but I haven't used my xslt skills in over seven years and am trying to help someone.

All help is appreciated!

Thanks,

Russ

1 Answers

Consider the following example:

XML

<root>
    <entry><p>Description</p></entry>
    <entry><p>Description2</p><p>Description3</p><p>Description4</p></entry>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="entry/p[1]"> 
    <xsl:apply-templates/>
</xsl:template> 

</xsl:stylesheet>

Result

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <entry>Description</entry>
    <entry>Description2<p>Description3</p><p>Description4</p></entry>
</root>
Related