【问题标题】:get href value from hyperlink in xslt [closed]从xslt中的超链接获取href值[关闭]
【发布时间】:2014-02-18 00:25:45
【问题描述】:

如果我从 xml 输入,我如何才能在 xslt 中仅获得 href 值。

<a href='http://google.com' target='_blank'>This is the heading </a>

我只需要href 值来创建链接。

【问题讨论】:

  • 你为什么不使用我在这里给出的答案:stackoverflow.com/questions/21328634/… - 而不是再次问完全相同的问题?
  • 我试过了,但根本不起作用,如果上面的链接给你href值,你可以试试
  • 它无法识别 href,因为它不是我的 XMl 中的单独文件,并且是超链接中标题标签的一部分
  • “它不识别href”是什么意思?还是通过“超链接中的部分标题标签”?
  • 我是 XSL 的新手。如果你能给我一个简单的工作版本,它会从上面的代码生成链接,那就太好了。我需要知道我们将如何调用此模板,在此先感谢您的帮助。

标签: c# asp.net xslt xml-parsing


【解决方案1】:

使用以下样式表。这正是我在你之前的问题中所描述的。

我需要知道我们将如何调用这个模板

匹配a 元素的模板由xsl:apply-templates 语句调用。

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:fo="http://www.w3.org/1999/XSL/Format">

   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <fo:root>

  <fo:layout-master-set>
    <fo:simple-page-master master-name="simple"
                  page-height="29.7cm"
                  page-width="21cm"
                  margin-top="1cm"
                  margin-bottom="2cm"
                  margin-left="2.5cm"
                  margin-right="2.5cm">
      <fo:region-body margin-top="3cm"/>
      <fo:region-before extent="3cm"/>
      <fo:region-after extent="1.5cm"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="simple">

    <fo:flow flow-name="xsl-region-body">

      <fo:block font-size="18pt"
            font-family="sans-serif"
            line-height="24pt"
            space-after.optimum="15pt"
            background-color="blue"
            color="white"
            text-align="center"
            padding-top="3pt">
        <xsl:apply-templates/>
      </fo:block>


      <!-- this defines normal text -->
      <fo:block font-size="12pt"
                font-family="sans-serif"
                line-height="15pt"
                space-after.optimum="3pt"
                text-align="justify">
        The Extensible Markup Language (XML) is a subset of SGML that is completely described in this document. Its goal is to
        enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML. XML
        has been designed for ease of implementation and for interoperability with both SGML and HTML.
      </fo:block>

    </fo:flow> <!-- closes the flow element-->
  </fo:page-sequence> <!-- closes the page-sequence -->
</fo:root>
   </xsl:template>

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

</xsl:stylesheet>

应用于您的输入 XML:

<a href='http://google.com' target='_blank'>This is the heading </a>

使用 XSLT 2.0、Saxon 和 Apache FOP 1.0,您会得到正确的输出,即可点击的标题:

【讨论】:

  • 非常感谢,它是 XSL 2.0 的一个特性,在 1.0 中不起作用吗?我在 1.0 中尝试过,因为我们有那个版本。
  • 应该也可以。只需将代码修改为version="1.0"。另外,请考虑接受这两个问题(在答案左侧打勾)。
  • 它仍然对我不起作用,有什么办法可以将我的 XML 和 XSL 文件发送给您,以便您检查吗?
  • 没有。我不愿意投入更多的解释,这很简单,我已经花了很多时间。请记住,人们正在免费帮助您。相反,请阅读有关 XSLT 的书籍或教程(显然,您还不太了解它)——您将能够理解我给出的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 2015-06-18
  • 2020-12-23
相关资源
最近更新 更多