本文介绍了Excel-使用FILTERXML从字符串中提取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在尝试更加熟悉将带分隔符的字符串更改为XML以与Excel的 FILTERXML 并检索那些感兴趣的子字符串.请注意,此功能可从Excel 2013中获得,而在Excel for Mac或Excel Online中不可用.

Lately I've been trying to get more familiar with the concept of changing a delimited string into an XML to parse with Excel's FILTERXML and retrieve those substrings that are of interest. Please note that this function came available from Excel 2013 and is not available on Excel for Mac nor Excel Online.

对于定界字符串,我的意思是普通句子中使用空格作为定界符或任何其他可用于定义字符串中子字符串的字符组合的内容.例如,让我们想象以下内容:

With a delimited string, I meant anything from a normal sentence using spaces as delimiters or any other combination of characters that could be used to define substrings within a string. For example let's imagine the following:

ABC|123|DEF|456|XY-1A|ZY-2F|XY-3F|XY-4f|xyz|123


因此,许多人都知道如何获取 nth 元素(例如:=TRIM(MID(SUBSTITUTE(A1,"|",REPT(" ",LEN(A1))),3*LEN(A1)+1,LEN(A1)))来检索456).或其他与LEN()MID()FIND()以及所有这些构造的组合,我们如何使用FILTERXML使用更具体的条件来提取所关注的子字符串并清理整个字符串?例如,如何检索:

So, where a lot of people know how to get the nth element (e.g.: =TRIM(MID(SUBSTITUTE(A1,"|",REPT(" ",LEN(A1))),3*LEN(A1)+1,LEN(A1))) to retrieve 456). Or other combinationes with LEN(), MID(), FIND() and all those constructs, how do we use FILTERXML to use more specific criteria to extract substrings of concern and clean up the full string? For example, how to retrieve:

  • 按位置元素
  • 数字或非数字元素
  • 本身包含子字符串的元素
  • 以子字符串开头或结尾的元素
  • 大写或小写的元素
  • 持有数字的元素
  • 唯一值
  • ...

推荐答案

Excel的FILTERXML使用XPATH 1.0,不幸的是,它并不像我们希望的那样多样化.此外,Excel似乎 不允许 允许返回返工的节点值,并且排他性地允许您按出现的顺序选择节点.但是,我们仍然可以使用很多功能.可以在此处找到更多信息.

Excel's FILTERXML uses XPATH 1.0 which unfortunately means it is not as diverse as we would maybe want it to be. Also, Excel seems to not allow returning reworked node values and exclusively allows you to select nodes in order of appearance. However there is a fair share of functions we can still utilize. More information about that can be found here.

该函数有两个参数:=FILTERXML(<A string in valid XML format>,<A string in valid XPATH format>)

比方说,单元格A1包含字符串:ABC|123|DEF|456|XY-1A|ZY-2F|XY-3F|XY-4f|xyz|123.为了创建有效的XML字符串,我们使用SUBSTITUTE将定界符更改为有效的结束标记和开始标记构造.因此,对于给定的示例,要获得有效的XML构造,我们可以这样做:

Let's say cell A1 holds the string: ABC|123|DEF|456|XY-1A|ZY-2F|XY-3F|XY-4f|xyz|123. To create a valid XML string we use SUBSTITUTE to change the delimiter to valid end- and start-tag constructs. So to get a valid XML construct for the given example we could do:

"<t><s>"&SUBSTITUTE(A1,"|","</s><s>")&"</s></t>"

出于可读性原因,我将使用 <XML> 一词作为上述占位符的上述结构.在下面的有效构造中,您可以找到其他有用的XPATH函数来过滤节点:

For readability reasons I'll refer to the above construct with the word <XML> as a placeholder. Below you'll find different usefull XPATH functions in a valid construct to filter nodes:

=FILTERXML(<XML>,"//s")

=FILTERXML(<XML>,"//s[position()=4]")

=FILTERXML(<XML>,"//s[position()<4]")

=FILTERXML(<XML>,"//s[position()=2 or position()>5]")

=FILTERXML(<XML>,"//s[last()]")

=FILTERXML(<XML>,"//s[position() mod 2 = 1]")

=FILTERXML(<XML>,"//s[position() mod 2 = 0]")

=FILTERXML(<XML>,"//s[number()=.]")

或者:

=FILTERXML(<XML>,"//s[.*0=0]")

=FILTERXML(<XML>,"//s[not(number()=.)]")

或者:

=FILTERXML(<XML>,"//s[.*0!=0)]")

=FILTERXML(<XML>,"//s[contains(., 'Y')]")

=FILTERXML(<XML>,"//s[not(contains(., 'Y'))]")

=FILTERXML(<XML>,"//s[starts-with(., 'XY')]")

=FILTERXML(<XML>,"//s[not(starts-with(., 'XY'))]")

=FILTERXML(<XML>,"//s[substring(., string-length(.) - string-length('F') +1) = 'F']")

=FILTERXML(<XML>,"//s[not(substring(., string-length(.) - string-length('F') +1) = 'F')]")

=FILTERXML(<XML>,"//s[starts-with(., 'X') and substring(., string-length(.) - string-length('A') +1) = 'A']")

=FILTERXML(<XML>,"//s[translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')=.]")

=FILTERXML(<XML>,"//s[translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')=.]")

=FILTERXML(<XML>,"//s[translate(.,'1234567890','')!=.]")

=FILTERXML(<XML>,"//s[translate(.,'1234567890','')=.]")

=FILTERXML(<XML>,"//s[translate(.,'1234567890','')!=. and .*0!=0]")

=FILTERXML(<XML>,"//s[preceding::*=.]")

=FILTERXML(<XML>,"//s[not(preceding::*=.)]")
=FILTERXML(<XML>,"//s[not(following::*=. or preceding::*=.)]")

=FILTERXML(<XML>,"//s[string-length()=5]")

=FILTERXML(<XML>,"//s[string-length()<4]")

=FILTERXML(<XML>,"//s[preceding::*[1]='456']")

=FILTERXML(<XML>,"//s[starts-with(preceding::*[1],'XY')]")

=FILTERXML(<XML>,"//s[following::*[1]='123']")

=FILTERXML(<XML>,"//s[contains(following::*[1],'1')]")

=FILTERXML(<XML>,"//s[preceding::*='ABC' and following::*='XY-3F']")

=FILTERXML(<XML>,"//s[substring-after(., '-') = '3F']")

=FILTERXML(<XML>,"//s[contains(substring-after(., '-') , 'F')]")

=FILTERXML(<XML>,"//s[substring-before(., '-') = 'ZY']")

=FILTERXML(<XML>,"//s[contains(substring-before(., '-'), 'Y')]")

=FILTERXML(<XML>,"//s[concat(., '|', following::*[1])='ZY-2F|XY-3F']")

=FILTERXML(<XML>,"//s[contains(concat(., preceding::*[2]), 'FA')]")

=FILTERXML(<XML>,"//s[count(node())>0]")

或者:

=FILTERXML(<XML>,"//s[node()]")

=FILTERXML(<XML>,"//s[count(node())=0]")

或者:

=FILTERXML(<XML>,"//s[not(node())]")

现在,显然上述内容是对XPATH 1.0功能可能性的演示,您可以获取上述内容的更多组合以及更多内容!我试图介绍最常用的字符串函数.如果您有任何遗漏,请随时发表评论.

Now obviously the above is a demonstration of possibilities with XPATH 1.0 functions and you can get a whole range of combinations of the above and more! I tried to cover most commonly used string functions. If you are missing any please feel free to comment.

这个问题本身很广泛,我希望就如何使用FILTERXML进行手边的查询给出一些一般性的指导.该公式返回要以任何其他方式使用的节点数组.很多时候,我会在TEXTJOIN()INDEX()中使用它.但是我想其他选择可能是可以溢出结果的新DA功能.

Whereas the question is quite broad on itself, I was hoping to give some general direction on how to use FILTERXML for the queries at hand. The formula returns an array of nodes to be used in any other way. A lot of the times I would use it in TEXTJOIN() or INDEX(). But I guess other options would be new DA-functions to spill results.

这篇关于Excel-使用FILTERXML从字符串中提取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:19