如果您使用的是 XSLT 2.0,则可以使用tokenize():
<xsl:template match="media:content">
<xsl:value-of select="tokenize(@url,'\?')[1]"/>
</xsl:template>
这是另一个仅更改media:content 的url 属性的示例:
<xsl:template match="media:content">
<media:content url="{tokenize(@url,'\?')[1]}">
<xsl:copy-of select="@*[not(name()='url')]"/>
<xsl:apply-templates/>
</media:content>
</xsl:template>
编辑
要处理实例中的所有 url 属性,并保持其他所有内容不变,请使用身份转换并仅使用@url 的模板覆盖它。
这是您的示例 XML 的修改版本。我在description 中添加了两个属性进行测试。 attr 属性应该保持不变,url 属性应该被处理。
XML
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<atom:link href="http://www.videojug.com/user/metacafefamilyandeducation/subscriptions.mrss" type="application/rss+xml" rel="self"/>
<title>How to and instructional videos from Videojug.com</title>
<!-- added some attributes for testing -->
<description attr="don't delete me!" url="http://www.test.com/foo?anotherquerystring">Award-winning Videojug.com has over 50k professionally-made instructional videos.</description>
<link>http://www.videojug.com</link>
<item>
<title>How To Calculate Median</title>
<media:content url="http://direct.someurl.com/54/543178dd-11a7-4b8d-764c-ff0008cd2e95/how-to-calculate-median__VJ480PENG.mp4?somequerystring" type="video/mp4" bitrate="1200" height="848"
duration="169" width="480">
<media:title>How To Calculate Median</media:title>
..
</media:content>
</item>
</channel>
</rss>
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!--Identity Transform-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@url">
<xsl:attribute name="url">
<xsl:value-of select="tokenize(.,'\?')[1]"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
输出(使用 Saxon 9.3.0.5)
<rss xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
version="2.0">
<channel>
<atom:link href="http://www.videojug.com/user/metacafefamilyandeducation/subscriptions.mrss"
type="application/rss+xml"
rel="self"/>
<title>How to and instructional videos from Videojug.com</title>
<!-- added some attributes for testing --><description attr="don't delete me!" url="http://www.test.com/foo">Award-winning Videojug.com has over 50k professionally-made instructional videos.</description>
<link>http://www.videojug.com</link>
<item>
<title>How To Calculate Median</title>
<media:content url="http://direct.someurl.com/54/543178dd-11a7-4b8d-764c-ff0008cd2e95/how-to-calculate-median__VJ480PENG.mp4"
type="video/mp4"
bitrate="1200"
height="848"
duration="169"
width="480">
<media:title>How To Calculate Median</media:title>
..
</media:content>
</item>
</channel>
</rss>