XML 中的内部和外部链接

Internal and External Links in XML

我正处于学习 XML 和 XSL 的第 3 天,非常感谢任何帮助以了解我做错了什么。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="styles.xsl"?>
<document>
  <toc>
    <heading>Table of Contents</heading>
    <list>
      <item><xref href="#Foreword">Foreword</xref></item>
    </list>
  </toc>
  <content>
    <section>
      <title id="Foreword">Foreword</title>
      <para>Please see <link dest="http://foo.com/">Foo</link> for additional information.</para>
    </section>
  </content>
</document>

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="document">
    <html>
      <head>
        <title>Online Document</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="heading">
    <h1><xsl:apply-templates/></h1>
  </xsl:template>
  <xsl:template match="list">
    <ul>
      <xsl:for-each select="item">
        <li><xsl:apply-templates/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
  <xsl:template match="title">
    <h2><xsl:apply-templates/></h2>
  </xsl:template>
  <xsl:template match="para">
    <p><xsl:apply-templates/></p>
  </xsl:template>
  <xsl:template match="xref">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="@href"/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
  </xsl:template>
</xsl:stylesheet>

我能够为内部超链接输出 HTML——到目前为止,一切顺利。当我尝试在 XML 文档中使用外部 URL 时,输出是一长串文本。

如何在 XML 中使用 URL 而不会破坏文档的其余部分?

对于外部 URL,此 XSL 是否接近于正确?

<xsl:template match="link">
  <a href="{@dest}"><xsl:apply-templates/></a>
</xsl:template>

你就在那里。只需添加此模板:

<xsl:template match="link">
  <a>
    <xsl:attribute name="href">
      <xsl:value-of select="@dest"/>
    </xsl:attribute>
    <xsl:value-of select="."/>
  </a>
</xsl:template>

产生:

<html>
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <title>Online Document</title>
 </head>
 <body>
   <h1>Table of Contents</h1>

   <ul>
      <li><a href="#Foreword">Foreword</a></li>
   </ul>
   <h2>Foreword</h2>
   <p>Please see <a href="http://foo.com/">Foo</a> for additional information.
   </p>
 </body>
</html>