is there a way to check if there is text between t

2019-09-02 05:56发布

问题:

I've the below 2 different XML Cases.

Case1

<para><content-style font-style="bold">1/3</content-style> This is text</para>

Case2

<para>This is text <content-style font-style="bold">1/3</content-style></para>

i'm using the template match as below

<xsl:template match="para[content-style[matches(., '(\w+)/(\w+)')]]">

but as per this match, both the cases mentioned above are satisfied, i want only first case to be caught ignoring the second case.

please let me know how can i get this done.

Thanks

回答1:

There is no need to use matches(). If the rule is whether the content-style element is the first child node of para, match for

para[node()[position() = 1 and self::content-style]]

Assuming the following input document, where both cases are present:

XML Input

<root>
    <para><content-style font-style="bold">1/3</content-style>YES</para>
    <para>NO <content-style font-style="bold">1/3</content-style></para>
</root>

XSLT Stylesheet

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="para[node()[position() = 1 and self::content-style]]">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:stylesheet>

XML Output

<?xml version="1.0" encoding="UTF-8"?>
<para>
   <content-style font-style="bold">1/3</content-style>YES</para>