2011-05-26 14 views
5

我需要從MRSS RSS源中的所有屬性執行查詢字符串的正則表達式樣式替換,並將它們剝離爲url。我試過幾件事情在這裏使用從這裏提示:XSLT Replace function not found但無濟於事XSLT從xml文件中的所有URL中刪除查詢字符串

<?xml version="1.0" encoding="utf-8"?> 
<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> 
<description>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> 

任何建議,真正幫助

+0

你只需要將url查詢部分刪除? – 2011-05-26 18:34:37

回答

3

如果您使用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> 
+0

ok - 看起來不錯,但是這個文件中可能還有其他的東西也有url屬性。我想修剪所有這些屬性值。如果我將匹配更改爲@url,它將匹配該屬性值(據我瞭解)我不清楚我如何確保當我寫回它只是覆蓋屬性並保留元素的其餘部分? – RichHalliwell 2011-05-27 09:27:22

+0

@RichHalliwell:您可以確保只使用標識轉換來處理其他所有元素(其他元素,屬性,文本等),以覆蓋url屬性。請參閱我的編輯示例。 – 2011-05-27 15:03:01

+0

另外,+1是一個很好的問題。 – 2011-05-27 15:04:18

2

字符串在XSLT 2.0中處理通常比使用XSLT 2.0更容易,但在這種情況下,使用自XSLT 1.0以來存在的substring-before()函數可以很容易地實現要求。

+2

'substring-before(concat(url,'?'),'?')'用於增加失效安全性。 – Tomalak 2011-05-27 15:06:14

相關問題