首页 \ 问答 \ 在XML文件中搜索多个元素(Searching Multiple Elements In XML File)

在XML文件中搜索多个元素(Searching Multiple Elements In XML File)

我正在使用此处的W3C Live Ajax搜索。 他们的代码只搜索一个元素,即“标题”。 我想让用户的查询和搜索多个元素,例如'title'和'url'。

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("live.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
  {
  $d=$x->item($i)->getElementsByTagName('title');
  $z=$x->item($i)->getElementsByTagName('url');
  if ($d->item(0)->nodeType==1)
    {
    //find a link matching the search text
    if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false)   
      {
      if ($hint=="")
        {
        $hint=
        $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . 
        $d->item(0)->childNodes->item(0)->nodeValue;
        }
      else
        {
        $hint=$hint . "<br /><br />" .
        $z->item(0)->childNodes->item(0)->nodeValue . "<br />" .  
        $d->item(0)->childNodes->item(0)->nodeValue;
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;

?> 

有人能给我一个如何让它工作的例子吗? 根据我的理解,stristr只搜索一个haystack,你不能使用数组,那么还有另一种方法吗? 提前致谢!


I'm using the W3C Live Ajax Search found here. Their code only searches one element, the "title". I would like to have it take the user's query and search through multiple elements, for example, the 'title' and the 'url'.

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("live.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
  {
  $d=$x->item($i)->getElementsByTagName('title');
  $z=$x->item($i)->getElementsByTagName('url');
  if ($d->item(0)->nodeType==1)
    {
    //find a link matching the search text
    if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false)   
      {
      if ($hint=="")
        {
        $hint=
        $z->item(0)->childNodes->item(0)->nodeValue . "<br />" . 
        $d->item(0)->childNodes->item(0)->nodeValue;
        }
      else
        {
        $hint=$hint . "<br /><br />" .
        $z->item(0)->childNodes->item(0)->nodeValue . "<br />" .  
        $d->item(0)->childNodes->item(0)->nodeValue;
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;

?> 

Can somebody give me an example of how to make it work? From what I understand, stristr will only search one haystack and you can't use arrays, so is there another way? Thanks in advance!


原文:https://stackoverflow.com/questions/13941629
更新时间:2021-08-04 10:08

最满意答案

Golang Tour指定导出的名称为

如果名称以大写字母开头,则会导出该名称。 并且在导入包时,您只能引用其导出的名称。 任何“未报告”的名称都不能从包装外部访问。

reverse func的名称更改为Reverse以使其可导出到主包。 如下所示

package stringutil

func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
} 

Golang Tour specify exported name as

A name is exported if it begins with a capital letter. And When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.

Change the name of reverse func to Reverse to make it exportable to main package. Like below

package stringutil

func Reverse(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
} 

相关问答

更多
  • 你可以使用asNamespace : > methods(cbind) [1] cbind.data.frame cbind.grobGrid cbind.ts* Non-visible functions are asterisked > r <- unclass(lsf.str(envir = asNamespace("stats"), all = T)) > r[grep("cbind.ts", r)] [1] ".cbind.ts" "cbind.ts" stats包中 ...
  • 这是一个标签声明 。 你可以用goto , break或continue使用它。 来自docs : 定义从未使用过的标签是非法的。 与其他标识符相比,标签不是块作用域,也不与非标签的标识符冲突。 标签的范围是声明它的函数体,并排除任何嵌套函数的主体。 它们在您需要以某种方式更改流量的情况下非常有用,即。 K: for i:=0; i<10; i++ { for j:=0; j<10; j++{ if (somefunction(j)) { continue K ...
  • Golang Tour指定导出的名称为 如果名称以大写字母开头,则会导出该名称。 并且在导入包时,您只能引用其导出的名称。 任何“未报告”的名称都不能从包装外部访问。 将reverse func的名称更改为Reverse以使其可导出到主包。 如下所示 package stringutil func Reverse(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 ...
  • 那里的散列函数通常需要一个ByteArrayAccess实例来输入它们。 只有hashlazy特别需要一个懒惰的ByteString 。 hash (hashlazy x)应该有效,因为Digest有一个ByteArrayAccess实例。 要将多个Digest一起散列,可以使用底层的hashInit / hashUpdates / hashFinalize函数,将所有Digest传递给hashUpdates 。 快速举例: import System.IO import Crypto.Hash impor ...
  • 由于您使用的是Roxygen和devtools,因此您需要执行以下操作: 为要从包中导出的每个函数添加一个#' @export指令 在构建/安装包之前运行document() ,以确保更新NAMESPACE文件。 Since you're using Roxygen and devtools, you need to do the following: Include a #' @export directive for every function you want to export from your ...
  • load_all函数有一个export_all参数。 来自?load_all 如果为TRUE(默认值),则导出所有对象。 如果为FALSE,则仅导出在NAMESPACE文件中列为导出的对象。 因此,请在您的load_all调用中尝试使用export_all=FALSE 。 The load_all function has an export_all argument. From ?load_all If TRUE (the default), export all objects. If FALSE, e ...
  • R不提供隐藏源代码的基础设施[1]。 这样做是违反R开发者的价值观和愿望的。 所以,你不想用R做什么。 [1] http://r.789695.n4.nabble.com/how-to-hide-code-of-any-function-td4474822.html R does not provide infrastructure for hiding source code[1]. Doing that is against the R developers' values and wishes. So ...
  • 从另外一些方面来看,它似乎是\docType{import} ,在R 3.1.1中添加,是处理这种情况的最佳可用机制( 如此roxygen2问题中所述 )。 这似乎有效地表现得像我上面的第三个选项,但它的优点是不能明确记录函数参数,因此它们不必保持同步。 看来roxygen2语法就好 #' @export RNifti::xform 生成正确形式的.Rd文件,并满足R CMD check 。 我以前没有使用过这种特殊的语法,这就是问题仍然悬而未决的原因。 我已经确认这适用于未经修改的第三方软件包,因此它看 ...
  • 哈希实现都是通过一个通用的hash.Hash 接口提供的 。 如果你查看sha512.New的文档,你会看到它返回一个hash.Hash ,它列出了所有Hash特定的方法。 hash.Hash接口中的第一个字段是嵌入式 io.Writer ,它提供了标准的Write方法。 所有这些值都是文档中的链接,您可以遵循所需的定义,甚至是源代码。 The hash implementations are all provided through a common hash.Hash interface. If you ...
  • 编辑:只使用在NAMESPACE导出的函数,并假设它们的Roxygen块中没有@export语句,我们可以对所有export函数进行hacky解析: NAMESPACE <- readLines("NAMESPACE") exported_fns <- grep( "^export\\(", NAMESPACE, value=TRUE ) exported_fn_names <- gsub( "export\\((.*)\\)", "\\1", exported_fns ) fn_match_re <- p ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。