Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...