javascript - 如何在 XML 中搜索关键字并向下滚动窗口找到它?

标签 javascript xml xslt

Thanks to Bishiboosh and fmsf. As suggested, we're going to do it through XSL which looks more obvious answer of the problem (for detail please read below question).

We'd have to create an XSL which will search a keyword and match with node name/value, attribute name/value etc. everything in XML.

XML

<?xml version="1.0"?>
<CustomerList>
  <Customer>
    <CustomerID>1111</CustomerID>
    <CompanyName>Sean Chai</CompanyName>
    <City>New York</City>
    <NewCustomer>N</NewCustomer>
    </Customer>
  <Customer>
    <CustomerID>1112</CustomerID>
    <CompanyName>Tom Johnston</CompanyName>
    <City>Los Angeles</City>
    <NewCustomer>N</NewCustomer>
  </Customer>
  <Customer>
    <CustomerID>1113</CustomerID>
    <CompanyName>Institute of Art</CompanyName>
    <City>Chicago</City>
    <NewCustomer>Y</NewCustomer>
  </Customer>
</CustomerList>

XSL

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:choose>
        <!-- 
        // We'll match the node/attribute name/values
        // with search text. If it is match to node 
        // name/value we have to put a div/b tag around
        // it or it is match to attribute then the div/b
        // tag will be around it's node.
        -->
        <xsl:when test="">
          <xsl:value-of select="" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

I am having a problem in completion of XSL. We'd like to have some help to get it done.

Thanks.

嗨,

我必须在产品的基于 Web 的帮助系统中添加一项功能。外观、感觉和功能几乎与我们在桌面软件帮助中看到的相同,按 F1 会弹出帮助窗口。

我必须添加的功能是搜索功能,其中用户输入文本,将调用 Ajax,请求将定向到服务器,在服务器中它将以包含列表的 XML 形式组成响应XML 模式。这已经完成了!除外。

让我先来看看应用程序 UI。因此,当我们讨论这个问题的技术细节时,您会有更好的想法。

            _________________________________________________ 
            | Search           |
            |__________________|    XML SCHEMA OPEN HERE ...
(1) FRAME A | Type here...     |       (2) FRAME B
            |__________________|
            |                  |
            | . RESULT LINK 1  |
            | . RESULT LINK 2  |
            | . RESULT LINK 3  |
            |                  |
            |                  |
            |                  |
            |                  |
            |                  |

它是如何工作的?搜索结果将显示为超链接(如上面的 RESULT LINK 1...3 所示)。当用户单击这些链接之一时,架构将加载到框架 B 中,并且浏览器滚动到搜索的关键字。

还有一件重要的事情要提一下,如果您需要知道的话,XML Schema 将在 Frame B 中直接引用。即

$(iframe).src = "XMLSchema.xml"

首先,假设几乎所有初始工作都已完成,例如搜索、处理、化妆结果链接,然后单击它们,我们将在正确的框架(框架 B)中获得正确的模式。

问题是,当 XML 模式加载到正确的框架中时,它也应该滚动并突出显示第一个搜索的关键字。

我认为不可能在那里突出显示,因为该文档是 XML 格式,并且正如我所说,框架 B 具有 XML 文件的直接引用。但是,搜索和滚动有相当的概率吗?是的?没有?

您知道如何滚动到 XML 文件中搜索的关键字吗?

谢谢。

P.S:如果您有任何疑问,请评论。

最佳答案

尝试使用 XSL 将 XML 转换为可见的内容。

对请求更多信息的评论的答复:

XSL 将 XML 转换为 HTML。之后,对于滚动部分,您可以使用 javascript 滚动到您想要的位置。

关于javascript - 如何在 XML 中搜索关键字并向下滚动窗口找到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2012960/

相关文章:

python - 将撒克逊与 python 一起使用

javascript - 如何使用 Javascript 中的 Fetch API 加载 ttf-fontfiles?

javascript - 使用javascript将数组重组为数组数组

java - 如何在 MVEL 表达式中使用 substring 字符串函数

xml - XSLT 删除所有属性的前导和尾随空格

xml - XSLT 删除空白命名空间而不将元素分配给不同的或默认命名空间

javascript - Chart.js 在固定时间尺度上绘制时间序列数据

javascript - Post Ajax 函数返回 true 或 false

xml - 如何为 XAML 创建我自己的(非 CLR!)XML 命名空间?

xml - 如何在 ActionScript 中动态编辑 XML 节点值?