微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSLT从xml文件中的所有URL中删除查询字符串

我需要从MRSS RSS提要中的所有属性执行查询字符串的正则表达式替换,将它们剥离到仅仅是url.我在这里尝试了一些使用建议的东西: XSLT Replace function not found但无济于事

<?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的url属性的示例: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的修改版本.我在测试描述中添加了两个属性.应该保持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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。