Xpath 和 XSLT 将选定的数据显示为 html [英] Xpath and XSLT to display selected data to html

查看:17
本文介绍了Xpath 和 XSLT 将选定的数据显示为 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 xml 创建 XSLT 我必须创建 Xpath 查询以搜索停靠点并以 html 格式显示并显示停靠点编号、名称、位置(纬度和经度)和路线编号.

Hi I am creating XSLT for xml I have to create Xpath query to search for stop and display in html format and display stop number, name, location (latitude&longitude), and route number.

我只想问问我的 XSLT 和 Xpath 查询是否正确

I just want to ask is my XSLT and Xpath Query is right

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <xsl:element name="html">
      <xsl:element name="body">
        <table style="width:720px" border="3">
          <tr>
            <th>
              <font face="Verdana" size="4">ltc Stop in </font>
            </th>
          </tr>
          <xsl:apply-templates select="//stop[name='']/name"/>
        </table>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template match="name">
    <tr>
      <td>
        <xsl:value-of select="."/>
      </td>
    </tr>
  </xsl:template>

</xsl:stylesheet>

XML

<allstops>
  <stop number="2504" name="Main &amp; Bainard EB">
    <location>
      <latitude>42.91033567</latitude>
      <longitude>-81.29671483</longitude>
    </location>
    <routes>28</routes>
  </stop>
  <stop number="20" name="Adelaide &amp; Ada NB">
    <location>
      <latitude>42.9742886</latitude>
      <longitude>-81.2252341</longitude>
    </location>
    <routes>16</routes>
  </stop>
  <stop number="22" name="Adelaide &amp; Central Ave NB">
    <location>
      <latitude>42.9945666</latitude>
      <longitude>-81.2343441</longitude>
    </location>
    <routes>16</routes>
  </stop>

推荐答案

正如 Jason 所说,您的 Xpath 正在引用 XML 中不存在的位置,没有 name 节点,只有一个会导致问题的属性.

As Jason stated your Xpath is referencing a location that doesn't exist in the XML, there is no name node, just an attribute which would have caused issues.

您可能想要做的是找到所有停靠点并处理它们,因此您只需转到 //stop 即可执行此操作.此外,使用已弃用的标签(例如 font)是一种不好的做法,因此我已将其删除.

What you likely want to do is find all the stops and process those, so you can just go //stop to do this. Also, its bad practise to use deprecated tags, like font so I've removed those.

下面是一个可以帮助您入门的示例 XSLT.

Below is an example XSLT that might help get you started.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <xsl:element name="html">
      <xsl:element name="body">
        <table style="width:720px" border="3">
      <tr>
            <td>Stop #</td>
            <td>Route #</td>
            <td>Name</td>
          </tr>
          <xsl:apply-templates select="//stop"/>
        </table>
      </xsl:element>
    </xsl:element>
  </xsl:template>
 <xsl:template match="stop">
    <tr>
      <td>
        <xsl:value-of select="@number"/>
      </td>
      <td>
        <xsl:value-of select="routes"/>
      </td>
      <td>
        <xsl:value-of select="@name"/>
      </td>
    </tr>
  </xsl:template>
 </xsl:stylesheet>

XML:

<allstops>
  <stop number="2504" name="Main and Bainard EB">
    <location>
      <latitude>42.91033567</latitude>
      <longitude>-81.29671483</longitude>
    </location>
    <routes>28</routes>
  </stop>
  <stop number="20" name="Adelaide and  Ada NB">
    <location>
      <latitude>42.9742886</latitude>
      <longitude>-81.2252341</longitude>
    </location>
    <routes>16</routes>
  </stop>
  <stop number="22" name="Adelaide and Central Ave NB">
    <location>
      <latitude>42.9945666</latitude>
      <longitude>-81.2343441</longitude>
    </location>
    <routes>16</routes>
  </stop>
</allstops>

输出:

<html>
<body>
<table style="width:720px" border="3">
<tr>
<td>Stop #</td>
<td>Route #</td>
<td>Name</td>
</tr>
<tr>
<td>2504</td>
<td>28</td>
<td>Main and Bainard EB</td>
</tr>
<tr>
<td>20</td>
<td>16</td>
<td>Adelaide and  Ada NB</td>
</tr>
<tr>
<td>22</td>
<td>16</td>
<td>Adelaide and Central Ave NB</td>
</tr>
</table>
</body>
</html>

示例输出:

这篇关于Xpath 和 XSLT 将选定的数据显示为 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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