首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT将RSS XML中的所有标记名称和属性小写

使用XSLT将RSS XML中的所有标记名称和属性小写
EN

Stack Overflow用户
提问于 2016-12-05 22:17:47
回答 1查看 979关注 0票数 1

我怎样才能转到下面(大写字母标签,即<RSS>和大写字母属性,即TYPE="audio/mpeg")

代码语言:javascript
复制
<RSS xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
    <CHANNEL>
        <title>Example Title</title>
        <LINK>Example Link</link>
        <atom:link HREF="http://example.com/feed" REL="self" TYPE="application/rss+xml"/>
        <itunes:subtitle>Example subtitle with itunes namespace</itunes:subtitle>
        <itunes:owner>
            <itunes:name>OWNER NAME</itunes:name>
            <itunes:email>owner@gmail.com</itunes:email>
        </itunes:owner>
        <ITEM>
            <TITLE>Title Name here</TITLE>
            <itunes:author>Author name here</itunes:author>
            <ENCLOSURE TYPE="audio/mpeg" URL="http://www.podtrac.com/abc.mp3" LENGTH="31805"/>
        </ITEM>
    </CHANNEL>
</RSS>

(小写字母标记,即<rss>和小写字母属性,即type="audio/mpeg")

代码语言:javascript
复制
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
    <channel>
        <title>Example Title</title>
        <link>Example Link</link>
        <atom:link href="http://example.com/feed" rel="self" type="application/rss+xml"/>
        <itunes:subtitle>Example subtitle with itunes namespace</itunes:subtitle>
        <itunes:owner>
            <itunes:name>OWNER NAME</itunes:name>
            <itunes:email>owner@gmail.com</itunes:email>
        </itunes:owner>
        <item>
            <title>Title Name here</title>
            <itunes:author>Author name here</itunes:author>
            <enclosure TYPE="audio/mpeg" url="http://www.podtrac.com/abc.mp3" length="31805"/>
        </item>
    </channel>
</rss>

使用XSLT?

备注:

  1. 需要是所有的标记和所有被小写的属性,而不仅仅是<RSS><rss>TYPE="audio/mpeg"type="audio/mpeg"的例子。
  2. 我将使用PHP来运行这个
  3. 请注意xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"<rss>标记中的第一行命名空间设置--这必须保持不变。
  4. 请注意iTunes名称空间项(如<itunes:name>OWNER NAME</itunes:name> )--它们必须保持完全相同。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-06 01:01:44

这在XSLT1.0中提供了所需的结果:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
    <xsl:output indent="yes" />
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <!-- lowercase'es all elements names and copies namespaces-->
    <xsl:template match="*">
        <xsl:variable name="rawName" select="substring-before(name(), ':')"/>
        <xsl:element name="{translate(name(), $uppercase, $smallcase)}" namespace="{namespace-uri()}">
            <xsl:copy-of select="namespace::*"/>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

    <!-- lowercase'es all attribute names -->
    <xsl:template match="@*">
        <xsl:attribute name="{translate(name(), $uppercase, $smallcase)}">
          <xsl:value-of select="." />
        </xsl:attribute>
    </xsl:template>

    <!-- copies the rest -->
    <xsl:template match="text() | comment() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40984366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档