从xslt中的超链接获取href值

时间:2014-01-27 08:39:34

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

如果我输入xml,如何在xslt中仅获取href值。

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

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

1 个答案:

答案 0 :(得分: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,您可以获得正确的输出,可点击的标题:

  

enter image description here

相关问题