Xpath and XSLT to display selected data to html

2019-08-07 21:55发布

问题:

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.

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>

回答1:

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.

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.

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

XSLT

<?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>

Output:

<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>

Example output:



标签: xml xslt xpath