如果您使用XSLT 2.0,您可以使用tokenize()
:
<xsl:template match="media:content">
<xsl:value-of select="tokenize(@url,'\?')[1]"/>
</xsl:template>
這裏是唯一改變url
屬性的media:content
另一個例子:
<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>
OUTPUT(使用撒克遜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>
你只需要將url查詢部分刪除? – 2011-05-26 18:34:37