1

How can I please turn the below (capital letter tags i.e. <RSS> and capital letter attributes i.e. TYPE="audio/mpeg")

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

Into this (lower case letter tags i.e. <rss> and lower case letter attributes i.e. type="audio/mpeg")

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

Using XSLT?

NOTES:

  1. Needs to be all tags and all attributes that are lowercased, not just my example ones of <RSS> to <rss>, and TYPE="audio/mpeg" to type="audio/mpeg"
  2. I will be using PHP to run this
  3. Please notice the line one namespace setup of xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" inside the <rss> tag -- this must remain
  4. Please notice the iTunes namespace items such as <itunes:name>OWNER NAME</itunes:name> - these must remain exactly the same
zx485
  • 28,498
  • 28
  • 50
  • 59
mrmrw
  • 120
  • 9
  • Check out http://stackoverflow.com/questions/586231/how-can-i-convert-a-string-to-upper-or-lower-case-with-xslt – Meyer Dec 05 '16 at 22:26
  • @SMeyer Unfortunately that does not work. It removes namespaces, so fails notes number 2 and 3 on my list at the end of my question – mrmrw Dec 05 '16 at 22:29
  • The link was mostly for the lowercasing functionality. I recommend that you post a minimal executable version of your XSLT code here, so that it is easier to see what is going wrong. Keep in mind that stackoverflow is not a code writing service. – Meyer Dec 05 '16 at 22:39
  • @SMeyer Thank you for the comments and help -- I am using XSLT code I found here -- http://stackoverflow.com/questions/22927363/how-to-convert-tags-in-all-tags-in-xml-to-lowercase-without-changing-case-of-atr -- which works in that it lowers the case of attributes and tags, BUT, it also removes / damages all namespaces – mrmrw Dec 05 '16 at 22:50
  • 2
    You are not supposed to copy and paste (and run) code you don't understand. Read the code. Understand what it does. Make the necessary changes. There are three templates in the XSLT of that answer. Three. It's not an impossible task to figure them out. – Tomalak Dec 05 '16 at 23:05
  • 2
    How is this different from your other question here http://stackoverflow.com/questions/40982847/lowercase-rss-feeds-tag-names-and-attributes-with-xslt-keeping-namespaces#40982847 ? – michael.hor257k Dec 05 '16 at 23:07
  • @michael.hor257k I know it isn't cool to double post questions, but I did in this instance as I felt I over complicated the question in the first post and more clearly asked it in a simpler way here. I hope that is OK. Please advise if you think I should delete one. I just felt the last one is too complex now. Same thing can be asked much simpler, as I did here. Really hoping someone can help, otherwise may have to resort to regex which isn't great for this situation! – mrmrw Dec 05 '16 at 23:17
  • There is (probably) an error in your desired result XML. The `/@TYPE` attribute is not lowercased. – zx485 Dec 06 '16 at 01:11

1 Answers1

3

This gives the desired result in XSLT-1.0:

<?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>
zx485
  • 28,498
  • 28
  • 50
  • 59
  • Absolutely incredible. Elegant, works perfectly. You are a genius! Thank you, very very much. I will repay you the best way I can -- by answering a whole bunch of other peoples questions on StackOverflow to spread the love. Thank you again! – mrmrw Dec 06 '16 at 03:06
  • Also is the key line ""? Is that what I was missing? – mrmrw Dec 06 '16 at 03:06
  • I really am very very appreciative of this :) – mrmrw Dec 06 '16 at 03:50
  • @mrmrw: Glad I could help. BTW you were missing a whole [mcve] ;-) – zx485 Dec 06 '16 at 04:17