s-miral
s-miral

Reputation: 1

Replace string from lowercase to uppercase in specific location

I have file, with tags. I need to change all the values of 'element name' from lowercase to uppercase. Where there are more than one variable in the tag, also keep on replacing only the 'element name' value.

Input File:

<element name="product-info">
  <element name="user-info" maxTimes="total-name">
    <maxHelp="user-help">

Requested Output File:

<element name="PRODUCT-INFO">
  <element name="USER-INFO" maxTimes="total-name">
    <maxHelp="user-help">

Upvotes: 0

Views: 171

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

After some googling and trial&error, XSLT:

$ cat file.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="element/@name">
        <xsl:attribute name="name">
            <xsl:value-of select="translate(., $lowercase, $uppercase)" />
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="product/@id"/>
</xsl:stylesheet>
$ cat file.xml
<root>
  <otherelement name="foo" />
  <element name="product-info">
    <element name="user-info" maxTimes="total-name">
      <element maxHelp="user-help">
      </element>
    </element>
  </element>
</root>
$ xsltproc file.xsl file.xml
<?xml version="1.0"?>
<root>
  <otherelement name="foo"/>
  <element name="PRODUCT-INFO">
    <element name="USER-INFO" maxTimes="total-name">
      <element maxHelp="user-help">
      </element>
    </element>
  </element>
</root>

Upvotes: 0

sborsky
sborsky

Reputation: 561

sed 's/\([ \t\n\r\f]\)name="\([^"]*\)"/\1name="\U\2"/g' inputfile > outputfile

Or - to make it easier to read, use extended regular expressions:

sed -E 's/(\sname=")([^"]+")/\1\U\2/g' inputfile > outputfile

Upvotes: 1

Related Questions