使用xsl从字符串中提取字符串

时间:2012-06-18 12:04:07

标签: xslt xslt-1.0

抱歉这个愚蠢的问题,但xsl 1.0中的字符串处理不是我的优点之一。 我正在寻找一种提取伪html标签之间的字符串的方法。 EG。

<xsl:variable name="test">
    This is a string <link>http://www.stackoverflow.com</link> and some more stuff here
</xsl:variable>

这个想法最终会包含另外两个包含的变量。

var1 = http://www.stackoverflow.com 和 var2 =这是一个字符串,还有一些东西在这里

就像我说的那样,抱歉这是一个暗淡的问题。感谢。

1 个答案:

答案 0 :(得分:1)

以下是如何执行此操作

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

 <xsl:variable name="test">
  This is a string <link>http://www.stackoverflow.com</link> and some more stuff here
 </xsl:variable>

 <xsl:variable name="vTestNodeset" select="document('')/*/xsl:variable[@name='test']"/>

 <xsl:variable name="var1" select="string($vTestNodeset/link)"/>

 <xsl:variable name="var2">
  <xsl:copy-of select="$vTestNodeset/text()"/>
 </xsl:variable>

 <xsl:template match="/">
     "<xsl:value-of select="$var1"/>"
============    
     "<xsl:value-of select="$var2"/>"
============    
     "<xsl:value-of select="normalize-space($var2)"/>"
 </xsl:template>
</xsl:stylesheet>

当对任何XML文档(未使用)应用此转换时,会生成所需结果:

     "http://www.stackoverflow.com"
============    
     "
  This is a string  and some more stuff here
 "
============    
     "This is a string and some more stuff here"