【问题标题】:XSLT finding links inside tags in XML documentsXSLT 在 XML 文档的标签内查找链接
【发布时间】:2010-06-29 16:00:33
【问题描述】:

有没有办法让 xsl 样式表识别链接出现在 xml 文档中的标记内并将其转换为工作链接?

例子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="guys.xsl"?>

<people>
   <person>
      <name>Guy 1</name>
      <bio>Guy 1 is a guy.</bio>
   </person>

   <person>
      <name>Guy 2</name>
      <bio>Guy 2 is from <a href="http://www.example.com">somewhere</a>.</bio>
   </person>
</people>

Guy 1 的简历应显示为常规文本,Guy 2 的简历应包含有效链接。

【问题讨论】:

  • 是的。如果您发布您的“guys.xsl”,我们可以告诉您为什么链接没有被呈现。
  • 另外,请告诉我们 XSL 输出的最终目的地——它会发送到浏览器还是其他渲染机制?
  • 好问题 (+1)。请参阅我的答案以获得完整的解决方案。

标签: xml xslt


【解决方案1】:

有没有办法拥有一个 xsl 样式表在链接时识别 出现在 xml 的标签内 文档并将其转化为工作 链接?

是的。这种转变

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
   <table border="1">
     <xsl:apply-templates/>
   </table>
 </xsl:template>

 <xsl:template match="person">
   <tr>
     <xsl:apply-templates/>
   </tr>
 </xsl:template>

 <xsl:template match="person/*">
   <td><xsl:copy-of select="node()"/></td>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<people>
   <person>
      <name>Guy 1</name>
      <bio>Guy 1 is a guy.</bio>
   </person>

   <person>
      <name>Guy 2</name>
      <bio>Guy 2 is from <a href="http://www.example.com">somewhere</a>.</bio>
   </person>
</people>

产生想要的结果

<table border="1">
   <tr>
      <td>Guy 1</td>
      <td>Guy 1 is a guy.</td>
   </tr>

   <tr>
      <td>Guy 2</td>
      <td>Guy 2 is from <a href="http://www.example.com">somewhere</a>.</td>
   </tr>
</table>

【讨论】:

  • 完美。这正是我想要的。
【解决方案2】:

如果您尝试以 html 格式显示它,这将开箱即用:

<html>
<body>
  <xsl:for-each select="people/person">
     <div>
       <xsl:value-of select="name"/>
     </div>
     <div>
       <xsl:copy-of select="bio"/>
     </div>
  </xsl:for-each>
</body>
</html>

编辑:将 value-of 更改为 copy-of。请参阅此讨论: How do I preserve markup tags?

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    相关资源
    最近更新 更多