当前位置 : 主页 > 网页制作 > xml >

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

来源:互联网 收集:自由互联 发布时间:2021-06-13
我需要从MRSS RSS提要中的所有属性执行查询字符串的正则表达式替换,将它们剥离到仅仅是url.我在这里尝试了一些使用建议的东西: XSLT Replace function not found但无济于事 ?xml version="1.0" e
我需要从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>
网友评论