如何从文本中提取超链接

时间:2014-10-14 16:23:54

标签: xml xslt xslt-1.0

我的xml有以下元素:

<output_citation>C. T. Pan, R. R. Nair, U. Bangert, Q. Ramasse, R. Jalil, R. Zan, C. R. Seabourne, and A. J. Scott. (2012). Nanoscale electron diffraction and plasmon spectroscopy of single- and few-layer boron nitride. <em>Physical Review B</em>, 85(4), 045440.  eScholarID:<a class="escholarid"
        href="http://www.blah.ac.uk/escholar/uk-ac-blah-scw:205189">205189</a> | DOI:<a class="doi" href="http://dx.doi.org/10.1103/PhysRevB.85.045440">10.1103/PhysRevB.85.045440</a></output_citation>

使用XSLT 1.0 我需要提取两个超链接并将它们显示为可点击链接。 我设法使用以下方法提取第一个:

<xsl:variable name="urlEscholarId" select="output_citation/a/@href"> </xsl:variable>
<xsl:variable name="labelEscholarId" select="substring-after($urlEscholarId,'scw:')">       </xsl:variable>
 <a>
<xsl:attribute name="href"> 
<xsl:value-of select="$urlEscholarId"/>
</xsl:attribute>
<xsl:value-of select="$labelDoiId"/>
</a>

这给了我:

<a href="http://www.blah.ac.uk/escholar/uk-ac-blah-scw:205189">205189</a>

我似乎无法提取第二个以及如何输出上述文本排除欧元?

非常感谢

1 个答案:

答案 0 :(得分:0)

注意:这些解决方案显示了如何单独执行任务。这可能适用于您现有的XSLT样式表,也可能不适用。如果没有,您必须透露更多代码。

1输出HTML链接

也许根本不需要for-each循环或变量(无论如何它们都在某种程度上违背了XSLT的功能)。要查找这两个链接,只需编写模板以匹配a元素,创建新的a元素(或复制现有元素)并复制href属性和原始文本内容{ {1}}元素。

我假设a属性不应出现在输出中。

<强>样式表

class

XML输出

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="a">
        <a>
            <xsl:copy-of select="@href|text()"/>
        </a>   
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:transform>

2仅输出文字内容

  

以及如何输出上面的文字排除网址?

这是一项不同的任务,但也很容易解决。这将输出所有文本,保存为<?xml version="1.0" encoding="utf-8"?> <a href="http://www.blah.ac.uk/escholar/uk-ac-blah-scw:205189">205189</a> <a href="http://dx.doi.org/10.1103/PhysRevB.85.045440">10.1103/PhysRevB.85.045440</a> 元素的子节点的文本节点。

<强>样式表

a

文字输出

  

℃。 T. Pan,R。R. Nair,U。Bangert,Q。Ramasse,R。Jalil,R。Zan,C。R. Seabourne和A. J. Scott。 (2012年)。单层和少层氮化硼的纳米级电子衍射和等离子体光谱。物理评论B,85(4),045440。eScholarID:| DOI: