XPATH 过滤掉带字母的记录 [英] XPATH To filter out records with letters

查看:29
本文介绍了XPATH 过滤掉带字母的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将执行搜索以确保字段中没有字母的 XPATH 表达式.例如输入 XML:

I am looking for an XPATH expression that will perform a search to ensure a field does not have a letter in it. For example input XML:

<?xml version="1.0" encoding="UTF-8"?>
<payload>
    <records>
        <record>
            <number>123</number>
        </record>
        <record>
            <number>456</number>
        </record> 
        <record>
            <number>78A</number>
        </record> 
    </records>
</payload>

我也希望它过滤掉第三个结果,因为它在标签中有一个字母.所以返回这个:

I want it too filter out the third result as it has a letter in the tag. So return this:

<?xml version="1.0" encoding="UTF-8"?>
<payload>
    <records>
        <record>
            <number>123</number>
        </record>
        <record>
            <number>456</number>
        </record> 
    </records>
</payload>

在简单的 XPATH 中可以做到吗?

Is that possible to do in a simple XPATH?

所以像 /payload/records/record[reg expression here?]

@Cylian

这就是我的意思:

<?xml version="1.0" encoding="UTF-8"?>
<payload>
    <records>
        <record>
            <number>123</number>
            <time>12pm</time>
            <zome>UK</zome>
        </record>
        <record>
            <number>456</number>
            <time>12pm</time>
            <zome>UK</zome>
        </record> 
        <record>
            <number>78A</number>
            <time>12pm</time>
            <zome>UK</zome>
        </record> 
    </records>
</payload>

推荐答案

XPath(1.0 和 2.0)是一种用于 XML 文档的查询语言.

XPath (both 1.0 and 2.0) is a query language for XML documents.

因此XPath 表达式只能选择节点集(或提取其他数据),而不能改变 XML 文档的结构(如删除节点).

因此,无法构建将提供的 XML 文档更改为所需文档的 XPath 表达式.

Therefore, it is not possible to construct an XPath expression that alters the provided XML document to the wanted one.

使用 XSLT 或 XQuery 可以轻松完成此任务(不太容易):

This task can easily be accomplished with XSLT or XQuery (not so easily):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="record[translate(number, '0123456789', '')]"/>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<payload>
    <records>
        <record>
            <number>123</number>
        </record>
        <record>
            <number>456</number>
        </record>
        <record>
            <number>78A</number>
        </record>
    </records>
</payload>

产生了想要的、正确的结果:

<payload>
   <records>
      <record>
         <number>123</number>
      </record>
      <record>
         <number>456</number>
      </record>
   </records>
</payload>

这篇关于XPATH 过滤掉带字母的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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