Search code examples
xslt-2.0

How match non string in xslt


I want to match non string in xslt 2.0.

<td><list>
        <list-item>
            <p>aaaa</p>
            <list>
                <list-item>
                    <p>bbb</p>
                </list-item>
            </list>
        </list-item>
    </list>
</td>

I have done a template like this

<xsl:template match="td">
    <para>
        <xsl:apply-templates/>
    </tps:p>
</xsl:template>

When input xml like below:

<td><list>
        <list-item>
            <p>aaaa</p>
            <list>
                <list-item>
                    <p>bbb</p>
                </list-item>
            </list>
        </list-item>
    </list>
kmkmkmk
</td>

Output(correct output):

<para>kmkmkmk</para>

When input xml like below (new line between td and list):

<td><list>
        <list-item>
            <p>aaaa</p>
            <list>
                <list-item>
                    <p>bbb</p>
                </list-item>
            </list>
        </list-item>
    </list>
</td>

Output(Incorrect output):

<para> </para>

But When input xml like below( no new line between td and list ):

<td><list>
        <list-item>
            <p>aaaa</p>
            <list>
                <list-item>
                    <p>bbb</p>
                </list-item>
            </list>
        </list-item>
    </list></td>

Output(correct output):

nothing display

I want to do when td has no any text nothing display and display only display td has text.

Why when there is a new line between <td> and <list> not working well and where is no new line working wells. How can I fix this?


Solution

  • Your samples are not minimal and complete but to transform a td element node which has non-whitespace text child contents you can use

    <xsl:template match="td[text()[normalize-space()]]">
      <para>
        <xsl:apply-templates/>
      </para>
    </xsl:template>
    

    Whether you additionally need <xsl:template match="td[not(text()[normalize-space()])]"/> or <xsl:template match="td[not(text()[normalize-space()])]"><xsl:apply-templates/></xsl:template> depends on how you set up the rest of the transformation.