xslt - add elements to text but remove the in between element

I am not worked with XSLT lot. But Somehow, I am struggling to get the expected output for the below items.

Input 1:

<name>xxxx <xsample>dddd</xsample> zzzz</name>

Output for 1:

<p><t>xxxx dddd zzzz</t></p> // here I don't want to wrap the tag

Input 2

<name>xxxx <ysample>dddd</ysample> zzzz</name>

Output for 2:

<p><t>xxxx </t><t>dddd</t><t> zzzz</t></p>

I have tried with the below xslt code:

<xsl:template match="name">
<p>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</p>
</xsl:template>
<xsl:template match="name/text()[not(parent::ysample)]">
<t><xsl:value-of select="."/></t>
</xsl:template>
<xsl:template match="name/ysample">
<t><xsl:value-of select="."/></t>
</xsl:template>

Anybody could you please help me with this?

Thanks, Kumar

Upvotes: 1

Views: 36

Answers (2)

Tim C
Tim C

Reputation: 70648

I think the problem is with this line

<xsl:template match="name//text()[not(parent::ysample)]">

There are two issues here

  1. name/text() matches text nodes that are direct children of name, and so the condition not(parent::ysample), which applies to the text node, will never be true as the parent will always be name
  2. This is maybe a typo, but you probably want to check for xsample here, to implement your logic, especially because you already have a template matching ysample

Try this line instead:

<xsl:template match="name//text()[not(parent::xsample)]">

Upvotes: 1

Rupesh_Kr
Rupesh_Kr

Reputation: 3445

You can also check this in XSLT 2.0 with grouping

<xsl:template match="name">
    <p>
        <xsl:for-each-group select="node()" group-adjacent="self::text() or self::xsample">
            <t>
                <xsl:value-of select="current-group()"/>
            </t>
        </xsl:for-each-group>            
    </p>
</xsl:template>

Upvotes: 0

Related Questions