XSLT删除xml文件中所有URL中的querystring

问题描述

|| 我需要对MRSS RSS提要中所有属性查询字符串进行正则表达式样式替换,将其剥离为url。我在这里尝试了一些建议,方法是:XSLT找不到替换功能,但无济于事
<?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>
任何建议真的有帮助     

解决方法

如果您使用的是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>
编辑 要处理实例中的所有“ 3”属性,并使其他所有属性保持不变,请使用标识转换,并仅使用“ 7”的模板对其进行覆盖。 这是示例XML的修改版本。我在
description
中添加了两个属性以进行测试。 ѭ9属性应保持不变,ѭ3属性应进行处理。 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>
    ,XSLT 2.0中的XSLT中的字符串处理通常要容易得多,但是在这种情况下,使用XSLT 1.0之后的substring-before()函数看起来很容易满足要求。