简体   繁体   中英

xsl:value-of not working as intended

Hey im new to xml and trying to retrieve the value of a phone number shown below.

raw data -

 <aboutus>
    <title>The about us page!</title><br/>
    <description>GameChat started in 1934 and was founded by Mr. Gary Cashew who established the business.  
    </description><br/>
    <contact>
        <phone>07642345537</phone><br/>
        <email>GameChat@queries.co.uk </email><br/>
        <post>12 Foxtrot Road, FI23 632</post><br/>
    </contact>
</aboutus>

Template trying to turn data into a table

<table border="1">
    <tr bgcolor="#9acd32">
      <th>Contact</th>
      <th>Information</th>
    </tr>
    <tr>
      <td>Phone</td>
      <td><xsl:value-of select="phone"/></td>
    </tr>
  </table>

When used my table is created but the value of phone is not entered. I've probably made an amateur mistake so any help would be greatful, thanks

all of my xlst code looks like this -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>

        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Contact</th>
            <th>Information</th>
          </tr>
          <tr>
            <td>Phone</td>
            <td>
              <xsl:value-of select="phone"/>
            </td>
          </tr>

        </table>
      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>

Try

<xsl:value-of select="/aboutus/contact/phone" />

as your template matches the root node, the context is positioned at the <aboutus> node, you need to get the path to <phone> correct as per the hierarchy (This is assuming that is all the XML and <aboutus> is the root node)

or

Don't care about the hierarchy and just want to select the <phone> tag whereever it is (assuming there is only one) try

<xsl:value-of select="//phone" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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