简体   繁体   English

从外部 XML 实时更新网站上的文本

[英]Update text on a website live from an external XML

I'm a back-end programmer tasked with creating a front-end element on a site.我是一名后端程序员,负责在网站上创建前端元素。

I have to take only two data points from an external XML file and have then both placed within the site's existing html, and have it update every few minutes.我只需要从外部 XML 文件中获取两个数据点,然后将它们都放置在站点现有的 html 中,并每隔几分钟更新一次。

The xml tree is large and has several parent elements, I only need two child elements from the first parent element "Main-Library" xml 树很大并且有几个父元素,我只需要第一个父元素“Main-Library”中的两个子元素

Here is what the xml looks like.这是 xml 的样子。 I compressed it so only the first child of the first two parents are shown:我压缩了它,所以只显示了前两个父母的第一个孩子:

<Root>
    <Main-Library>
            <Book>
                <Title>Book Name</Title>
                <Author>John Smith</Author>
                <Genre>Fiction</Genre>
                <Loaned>2020-09-03 13:13:48</Loaned>
                <Price>-13.30</Price>
            </Book>
            .
            .
            .
    </Main-Library>
    <Second-Library>
            <Book>
                <Title>Book Name</Title>
                <Author>John Smith</Author>
                <Genre>Fiction</Genre>
                <Loaned>2020-09-03 13:13:48</Loaned>
                <Price>-13.30</Price>
            </Book>
            .
            .
            .
    </Second-Library>
</Root>

I need to take Loaned and Price from the first parent element Main-Library and place them within the following html:我需要从第一个父元素 Main-Library 中获取 Loaned 和 Price 并将它们放在以下 html 中:

<p class="feed">
<span class="feed-text-large">Status: </span>
<span class="price">$35.98</span>
<span class="timestamp">Loaned at 13:13 on September 03, 2020</span>
</p>

with price and the date coming from the XML, and refreshing as the xml changes.价格和日期来自 XML,并随着 xml 的变化而刷新。

Any help to get me started on this will be greatly appreciated.任何帮助我开始这方面的帮助将不胜感激。 Thanks!谢谢!

First of all - is it possible to use JSON?首先 - 是否可以使用 JSON? If yes, go for it and show how the data looks in JSON.如果是,请查看并显示数据在 JSON 中的外观。

Steps to do:操作步骤:

  1. Create a function that will retrieve data创建一个将检索数据的函数
function getData() {
  return fetch(theUrl).then(response => response.text())
}
  1. Create a function that will parse the data创建一个将解析数据的函数
function parseData(str) {
  const data = (new window.DOMParser()).parseFromString(str, 'text/xml')
  // never did anything with XML so this might be wrong
  // that's why JSON would be A LOT EASIER
  // this should get the first Book
  const Book = data.querySelector('Root Main-Library Book')
  const price = Book.querySelector('Price').innerHTML
  const loaned = Book.querySelector('Loaned').innerHTML
  return { price, loaned }
}
  1. Print the data打印数据
function printData(data) {
  const priceTarget = document.querySelector('.price')
  const loanedTarget = document.querySelector('.timestamp')
  priceTarget.innerHTML = data.price
  loanedTarget = data.loaned
}
  1. Create a function that will be run periodically创建一个将定期运行的函数
function update() {
  getData().then(parseData).then(printData)
}
  1. Run the function once and set an interval运行一次函数并设置一个间隔
const minutes = 5
update()
setInterval(update, minutes * 60 * 1000) // interval in ms, so 60*1000 gets us a minute

I'm not 100% sure if it will work - my main concern is parseData() because I've never done anything with XML in JavaScript.我不是 100% 确定它是否会起作用 - 我主要关心的是parseData()因为我从未在 JavaScript 中使用 XML 做过任何事情。

EDIT: That code will read data from first Book element.编辑:该代码将从第一个 Book 元素读取数据。 If you want to list all Books, the code will have to be different.如果要列出所有书籍,代码必须不同。 Let me know, I'll answer you in about 8 hours if no one will be quicker than me.让我知道,如果没有人比我更快,我会在大约 8 小时内回复您。 Goodnight晚安

With Saxon-JS you could do this by running an XSLT 3.0 stylesheet along the following lines:使用 Saxon-JS,您可以按照以下几行运行 XSLT 3.0 样式表来完成此操作:

<xsl:template name="display">
  <xsl:result-document href="#timestamp">
    <xsl:text>Loaned at {/Root/Main-Library/Book/Loaned
                         => translate(' ', 'T')
                         => xs:dateTime()
                         => format-date('[H00].[m00] on [MNn] [D1o], [Y0001]')
                        }</xsl:text>
  </xsl:result-document>
  <xsl:result-document href="#price">
    <xsl:text>${/Root/Main-Library/Book/Price}</xsl:text>
  </xsl:result-document> 
  <ixsl:schedule-action wait="60000">
    <xsl:call-template name="refresh"/>
  </ixsl:schedule-action>
</xsl-template>

<xsl:template name="refresh">
  <ixsl:schedule-action http-request="....">
    <xsl:call-template name="display"/>
  </ixsl:schedule-action>
</xsl-template>

This is just a sketch to illustrate the idea if you want to go down this route.如果您想沿着这条路线走下去,这只是一个草图来说明这个想法。 The main benefit (I know it appeals to some more than others) is that there's no Javascript to get wrong.主要的好处(我知道它比其他人更吸引人)是没有 Javascript 会出错。

Disclaimer: Saxon-JS is my company's product.免责声明:Saxon-JS 是我公司的产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM