XSL media:content - Trimming strings in XSLT

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

Can anyone tell me how to select the URL from this:

<media:content url="http://feedproxy.google.com/~r/TEDTalks_video/~5/aZWq6PY05YE/TimBrown_2009G.mp4" fileSize="57985745" type="video/mp4" />

I want to:

  1. Create a link to this file:

  2. Trim the URL from:

    http://feedproxy.google.com/~r/TEDTalks_video/~5/aZWq6PY05YE/TimBrown_2009G.mp4

to:

TimBrown_2009G

and then take: TimBrown_2009G and use it as part of a URL

回答1:

Selecting the URL. You just need to make sure that you have the correct namespace URI.

<xsl:value-of xmlns:media="http://search.yahoo.com/mrss/"                select="media:content/@url"/>

Trimming down the URL. How to do this best depends on whether you are using XSLT 1 or 2, since the latter has better string handling functions from XPath 2.0.

If you are using XSLT 1, you might want to create a helper template to return the last segment out of a delimited string:

<xsl:template name="last-substring-after">   <xsl:param name="string"/>   <xsl:param name="separator"/>   <xsl:choose>     <xsl:when test="contains($string, $separator)">       <xsl:call-template name="last-substring-after">         <xsl:with-param name="string"                         select="substring-after($string, $separator)"/>         <xsl:with-param name="separator"                         select="$separator"/>       </xsl:call-template>     </xsl:when>     <xsl:otherwise>       <xsl:value-of select="$string"/>     </xsl:otherwise>   </xsl:choose> </xsl:template>

Then you can make use of that to extract the last segment of the URL, and proceed to extract the part before the dot. Assuming the URL is in variable url:

<xsl:variable name="name">   <xsl:call-template name="last-substring-after">     <xsl:with-param name="string" select="$url"/>     <xsl:with-param name="separator" select="'/'"/>   </xsl:call-template> </xsl:variable> <xsl:value-of select="substring-before($name, '.')"/>


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!