XSLT从xml文件中的所有URL中删除查询字符串
我需要从MRSS RSS提要中的所有属性中执行一个正则表达式样式的QueryString替换,将它们拆分为url。我在这里尝试了一些使用建议的方法:但是没有用XSLT从xml文件中的所有URL中删除查询字符串,xml,regex,xslt,replace,Xml,Regex,Xslt,Replace,我需要从MRSS RSS提要中的所有属性中执行一个正则表达式样式的QueryString替换,将它们拆分为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>
<?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>
如何从Videojug.com下载视频和教学视频
屡获殊荣的Videojug.com拥有超过5万部专业制作的教学视频。
http://www.videojug.com
如何计算中位数
如何计算中位数
..
任何真正有用的建议如果您使用的是XSLT 2.0,您可以使用
tokenize()
:
编辑
<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>
要处理实例中的所有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>
如何从Videojug.com下载视频和教学视频
屡获殊荣的Videojug.com拥有超过5万部专业制作的教学视频。
http://www.videojug.com
如何计算中位数
如何计算中位数
..
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)
如何从Videojug.com下载视频和教学视频
屡获殊荣的Videojug.com拥有超过5万部专业制作的教学视频。
http://www.videojug.com
如何计算中位数
如何计算中位数
..
XSLT中的字符串处理在XSLT 2.0中通常要容易得多,但在这种情况下,使用子字符串-before()来实现要求似乎很容易自XSLT 1.0以来出现的函数。是否只需要删除url查询部分?好-看起来不错,但此文件中可能还有其他东西也具有url属性。我想修剪所有这些属性值。如果我将匹配更改为@url,它将只匹配该属性值(据我所知)我不清楚如何确保当我写回它时,它只会覆盖属性并保留元素的其余部分?@RichHalliwell:您可以通过使用标识转换来处理所有其他内容(其他元素、属性、文本等),从而确保只覆盖url属性。请参见我的编辑以获取示例。substring before(concat(url,“?”),“?”)
以获得更多的故障安全性。
<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>
<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>