【问题标题】:XSLT to remove querystring from all urls in an xml fileXSLT 从 xml 文件中的所有 url 中删除查询字符串
【发布时间】:2011-09-02 18:46:19
【问题描述】:

我需要对 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>

任何真正有用的建议

【问题讨论】:

  • 只需要去掉url查询部分吗?

标签: xml regex xslt replace


【解决方案1】:

如果您使用的是 XSLT 2.0,则可以使用tokenize()

  <xsl:template match="media:content">
    <xsl:value-of select="tokenize(@url,'\?')[1]"/>
  </xsl:template>

这是另一个仅更改media:contenturl 属性的示例:

  <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>

输出(使用 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>

【讨论】:

  • ok - 看起来不错,但此文件中可能还有其他内容也具有 url 属性。我想修剪所有这些属性值。如果我将匹配更改为@url,它将只匹配该属性值(据我所知)我不清楚我如何确保当我写回它时它只会覆盖属性并保留元素的其余部分?
  • @RichHalliwell:您将确保只覆盖 url 属性,方法是使用身份转换来处理其他所有内容(其他元素、属性、文本等)。请参阅我的编辑示例。
【解决方案2】:

使用 XSLT 2.0 在 XSLT 中处理字符串通常要容易得多,但在这种情况下,使用从 XSLT 1.0 开始出现的 substring-before() 函数看起来很容易实现要求。

【讨论】:

  • substring-before(concat(url, '?'), '?') 用于增加故障安全。
猜你喜欢
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 2016-07-22
  • 2011-03-28
  • 2011-02-26
  • 2018-12-08
相关资源
最近更新 更多