【问题标题】:Parsing an mp3 file name from a url using regex in xsl在 xsl 中使用正则表达式从 url 解析 mp3 文件名
【发布时间】:2013-01-17 06:42:24
【问题描述】:

我有一个 RSS 提要,需要将其转换为不同的 XML 模式。同时,将 mp3 文件名解析成一个新的字段。我有一个似乎适用于其余转换的 xsl(为我编写的),但我不知道如何做到这一点:

    <enclosure url="http://www.fffff.com/pts/redirect.mp3/audio.xxyy.org/musiccheck/musiccheck20130118pod.mp3" length="0" type="audio/mpeg"></enclosure>

变成这样:

    <fileIdentifier source="Theme">musiccheck20130118pod</fileIdentifier>

【问题讨论】:

    标签: xml regex xslt xml-parsing


    【解决方案1】:

    这是 XSLT 1.0 中不使用正则表达式的完整解决方案:

    <xsl:template match="enclosure">
    <xsl:variable name="url" select="./@url"/>
    <xsl:variable name="name">
      <xsl:call-template name="last-substring-after">
        <xsl:with-param name="string" select="$url"/>
        <xsl:with-param name="separator" select="'/'"/>
      </xsl:call-template>
    </xsl:variable>
    <fileIdentifier source="Theme">
    <xsl:value-of select="substring-before($name, '.')"/>
    </fileIdentifier>
    </xsl:template>
    
    
    <xsl:template name="last-substring-after">
      <xsl:param name="string"/>
      <xsl:param name="separator"/>
      <xsl:choose>
        <xsl:when test="contains($string, $separator)">
          <xsl:call-template name="last-substring-after">
            <xsl:with-param name="string"
                            select="substring-after($string, $separator)"/>
            <xsl:with-param name="separator"
                            select="$separator"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$string"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    输出将是-

    <fileIdentifier source="Theme">musiccheck20130118pod</fileIdentifier> 
    

    Source

    【讨论】:

      【解决方案2】:

      对你的 URL 格式做一些假设,即文件名是最后一个包含 .mp3 的东西:

      /[\S\s]*?url="[\S\s]*?\/([\w]+?\.mp3)".*?/$1/
      

      只要注意你想要得到的第二部分,就是:

      /[\S\s]*?url="[\S\s]*?\/([\w]+?\.mp3)".*?/<fileIdentifier source="Theme">$1</fileIdentifier>/
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 2011-03-20
        • 2017-02-02
        • 2010-09-18
        相关资源
        最近更新 更多