is there a way to check if there is text between the nodes

偶尔善良 提交于 2019-12-12 05:36:27

问题


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>


来源:https://stackoverflow.com/questions/28194842/is-there-a-way-to-check-if-there-is-text-between-the-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!