有没有办法检查节点之间是否有文本 [英] is there a way to check if there is text between the nodes

查看:38
本文介绍了有没有办法检查节点之间是否有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 2 个不同的 XML 案例.

I've the below 2 different XML Cases.

案例 1

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

案例 2

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

我使用的模板匹配如下

<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.

谢谢

推荐答案

没有必要使用 matches().如果规则是content-style元素是否为para的第一个子节点,则匹配

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 输入

<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 样式表

<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 输出

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

这篇关于有没有办法检查节点之间是否有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆