2011-05-26 18 views
5

que necesito para realizar un habitual de reemplazo estilo expresión de cadenas de consulta de todos los atributos en un MRSS RSS, despojándolos hasta sólo la url. He intentado un par de cosas aquí usando sugiere desde aquí: XSLT Replace function not found pero fue en vanoXSLT para eliminar la cadena de consulta de todas las URL en un archivo XML

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

alguna sugerencia muy útil

+0

¿Necesita sólo la parte de consulta url para ser eliminado? –

Respuesta

3

Si está utilizando XSLT 2.0, puede utilizar tokenize():

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

He aquí otro ejemplo de tan sólo cambiando el atributo url de media: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> 

EDITAR

Para manejar todos url atributos en su ejemplo, y dejar todo lo demás sin cambios, utilice una identidad única transformar y anularlo con una plantilla para @url.

Aquí es una versión modificada de su XML de ejemplo. He agregado dos atributos a description para probar. El atributo attr no debe tocarse y se debe procesar el atributo 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> 

SALIDA (Usando 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> 
+0

bien - se ve bien, pero puede haber otras cosas en este archivo que también tengan atributos de URL. Quiero recortar TODOS estos valores de atributo. si cambio la coincidencia a @url, solo coincidirá con ese valor del atributo (según lo entiendo). No estoy seguro de cómo puedo asegurarme de que cuando lo escriba, simplemente sobrescribe el atributo y conserva el resto del elemento. – RichHalliwell

+0

@RichHalliwell: Debería asegurarse de sobrescribir únicamente el atributo url utilizando una transformación de identidad para manejar todo lo demás (otros elementos, atributos, texto, etc.). Por favor, mira mi edición para ver un ejemplo. –

+0

Además, +1 para una buena pregunta. –

2

Cadena El manejo en XSLT generalmente es mucho más fácil con XSLT 2.0, pero en este caso parece bastante fácil lograr el requisito utilizando la función substring-before() que está presente desde XSLT 1.0.

+2

'substring-before (concat (url, '?'), '?')' Para mayor seguridad contra fallas. – Tomalak

Cuestiones relacionadas