Heapify
Heapify

Reputation: 2901

Simple XSLT Transformation When XML elements have text nodes and child nodes

I am new to xslt and am trying to transform the following xml:

<li>Hi there <person>David</person> , how is it going? </li>

I would like to transform this to another xml to something like:

<response>Hi there PERSON_NAME , how is it going? </response>

What I have so far is this:

<xsl:template match="li">
     <response><xsl:value-of select="text()"/>
     <xsl:choose>
           <xsl:when test="person">
                <xsl:text> PERSON_NAME </xsl:text>
           </xsl:when>
     </xsl:choose>
     </response> 
</xsl:template>

This is the output I get:

<response>Hi there , how is it going? PERSON_NAME</response>

Not exactly what I wanted. I am new to xslt and read a book. I did not find any example where there was a situation where an xml element had a child node in between its text value. Not sure if xslt can handle this or I am missing something fundamental. Any help would be greatly appreciated. I am using xslt 2.0

Upvotes: 0

Views: 88

Answers (1)

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

You can simply define two template to handle your condition.

<xsl:template match="li">
    <response>
        <xsl:apply-templates/>
    </response>
</xsl:template>

<xsl:template match="person">
    <xsl:text>PERSON_NAME</xsl:text>
</xsl:template>

Upvotes: 0

Related Questions