在Xpath中获取以大写字母开头的标签(PHP) [英] Get tags that start with uppercase in Xpath (PHP)

查看:341
本文介绍了在Xpath中获取以大写字母开头的标签(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用在XPath中注册的php函数来测试它,但该函数以小写形式接收第一个参数tagNames。



xml:

 < test> 
< A>符合此条件< / A>
< b>不符合这个< / b>
< / test>

php函数:

registerPhpFunctions - phpDoc

 ... 
public function isUpper($ name){
return(bool)preg_match('/ ^ [AZ] /',$ name);


Ant这是Xpath:

  // * [php:function(\Cdr\Dom\DOMXPath :: isUpper,name())] 

函数 isUpper 以小写形式接收$ name,因此不起作用。



我的问题是:


  1. 为什么不区分大小写?

    li>
  2. 有更好的方法可以做到这一点吗?
  3. > 使用这一行代码

      // * [包含('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring( name(),1,1))] 

    这选择XML文档中的任何元素,



    基于XSLT的验证

     < xsl:stylesheet version =1.0
    xmlns:xsl =http://www.w3.org/1999/XSL/Transform >
    < xsl:output omit-xml-declaration =yesindent =yes/>

    < xsl:template match =/>
    // * [contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring(name(),1,1))]/>
    < / xsl:template>
    < / xsl:stylesheet>

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

     < test> 
    < A>符合此条件< / A>
    < b>不符合这个< / b>
    < / test>

    对XPath表达式进行评估,并且选中的节点(本例中只有一个)被复制到输出

     < A>匹配此< / A> 


    I'm trying to get html tags that start with uppercase using DOMDocument in PHP 5.3.

    I'm using a php function registered in XPath to test it, but the function is receiving as first parameters tagNames in lowercase.

    The xml:

     <test>
         <A>Match this</A>
         <b>Dont match this</b>
     </test>
    

    The php function:

    registerPhpFunctions - phpDoc

    ...
    public function isUpper($name) {
        return (bool)preg_match('/^[A-Z]/', $name);
    }
    ...
    

    Ant this is the Xpath:

    //*[php:function("\Cdr\Dom\DOMXPath::isUpper", name())]
    

    the function isUpper receives $name in lowercase so it don't works.

    My questions are:

    1. Why isn't case sensitive?
    2. There is a better way to do this?

    解决方案

    Use this one-liner:

    //*[contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(name(),1,1))]
    

    this selects any element in the XML document, the first character of whose name is contained in the string of all capital letters.

    XSLT - based verification:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
      "//*[contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ', substring(name(),1,1))]"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <test>
        <A>Match this</A>
        <b>Dont match this</b>
    </test>
    

    the XPath expression is evaluated and the selected nodes (in this case just one) are copied to the output:

    <A>Match this</A>
    

    这篇关于在Xpath中获取以大写字母开头的标签(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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