【问题标题】:How to add clickable link to XML?如何将可点击的链接添加到 XML?
【发布时间】:2018-04-12 12:09:42
【问题描述】:

我正在尝试使我的 XML 文件中的 URL 可点击。下面是我的 XML 和 XSL 文件,它们可以很好地协同工作。

我尝试在 XML 文件中使用 XLink 和 href,但没有成功。

如何使 XML 文件中的 URL 可点击?

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file.xsl" ?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="1.xsl" ?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Johnny</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<url>http://www.yahoo.com</url>
</cd>
</catalog>

XSL

<?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>
<h2>XSL</h2>
<table border="1">
<tr bgcolor="#9acd32">
  <th style="text-align:left">Title</th>
  <th style="text-align:left">Artist</th>
  <th style="text-align:left">Country</th>
  <th style="text-align:left">Company</th>
  <th style="text-align:left">Price</th>
  <th style="text-align:left">URL</th>

</tr>
<xsl:for-each select="catalog/cd">
<tr>
  <td><xsl:value-of select="title"/></td>
  <td><xsl:value-of select="artist"/></td>
  <td><xsl:value-of select="country"/></td>
  <td><xsl:value-of select="company"/></td>
  <td><xsl:value-of select="price"/></td>
  <td><xsl:value-of select="url"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

【问题讨论】:

    标签: html xml xslt hyperlink xlink


    【解决方案1】:

    在编写任何 XSLT 之前,您应该真正知道您想要什么输出。要使链接在 HTML 中可点击,您可以这样做...

    <a href="http://www.yahoo.com">http://www.yahoo.com</a>
    

    所以,在您的 XSLT 中,不要这样做......

     <td><xsl:value-of select="url"/></td>
    

    这样做...

    <td>
        <a href="{url}">
           <xsl:value-of select="url"/>
        </a>
    </td>
    

    注意href 属性中大括号的使用。这称为属性值模板。花括号表示要计算的表达式,而不是字面输出。

    【讨论】:

    • 谢谢。我试试看。
    猜你喜欢
    • 2017-09-08
    • 2018-06-10
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2021-11-28
    • 2021-02-27
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多