Replace colon characters in XML tag attribute

Hi All,

I am converting XML to a JavaScript object in Javascript. I am 95% there using an external library for the grunt of the work. But I have noticed that some of the tag names include illegal characters such as the colon (:) if I want to use access their respective properties in Javascript using dot notation.

Here is as example of my XML and the output in JSON:

XML

<c:strRef>
                            <c:f>SheetTwo!$A$1</c:f>
                            <c:strCache>
                                <c:ptCount val="1"/>
                                <c:pt idx="0">
                                    <c:v>Hits</c:v>
                                </c:pt>
                            </c:strCache>
                        </c:strRef>

Open in new window


JSON

{
                            "c:strRef": {
                              "c:f": "SheetTwo!$A$1",
                              "c:strCache": {
                                "c:ptCount": {
                                  "val": 1
                                },
                                "c:pt": {
                                  "idx": 0,
                                  "c:v": "Hits"
                                }
                              }
                            }
                          }

Open in new window


What I would like to do in JavaScript is search and replace the colons from tag names only from my XML based string variable. This need to exclude replace colons from tag values (i.e. content should be preserved).

Please could anyone suggest a solution.

Thank you,

Rit
LVL 1
rito1Asked:
Who is Participating?
 
Rainer JeschorConnect With a Mentor Commented:
Hi,
no problem. I updated the regex for the tagopen (added the colon):
var regexTagOpen = /(<\w+)(:)([\w\s=":]+>)/gi;

Open in new window

and I updated the jsFiddle as well:
http://jsfiddle.net/EE_RainerJ/rb3q9cc4/

HTH
Rainer
0
 
Rainer JeschorCommented:
Hi,
this should be possible using "simple" replace.
Question: which library are you using to convert XML to JSON?
We will have to adjust this during the processing of the XML.

The colon in XML is not illegal as it is used to address namespaces inside the XML.

HTH
Rainer
0
 
rito1Author Commented:
Hi Rainer,

Illegal was probably a strong word in my context :-)

I'm using a node package call xml2json.

I read my XML file into a variable and call toString() on it.

I was using the following but think this is a little too aggressive as it will replace all colon within my XML rather than just colons within my XML tag.

myXml.toString().replace(/r:/g, '')

Thanks,
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Rainer JeschorCommented:
Hi,
OK, that regex might be a little bit too aggressive :-)

An easy fix would be to replace:
"<c:" with "<c_" and
"</c:" with "</c_"
Then you still have a valid XML document which should then be parsed successfully.

HTH
Rainer
0
 
rito1Author Commented:
Rainer, I appreciate your help.

I had only given you a snippet of my XML... my tags have different forms e.g. may not be <c:.. Ideally I would just want to detect and remove just colons rather than matching longer strings.

thanks,
Rit
0
 
Rainer JeschorCommented:
Hi,
OK, this should do the trick:
var regexTagOpen = /(<[a-z]+)(:)([a-z]+>)/gi;
var regexTagClose = /(<\/[a-z]+)(:)([a-z]+>)/gi
var inputstring = "<a:test>just</a:test><even:more>Testing::has:to:come</even:more>";
var newOut = "";
newOut = inputstring.replace(regexTagOpen,"$1$3");
newOut = newOut.replace(regexTagClose,"$1$3");

Open in new window


It uses the matching groups and eliminates both in the opening as in the closing tags all colons and replaces this with "".
Online testground:
http://jsfiddle.net/EE_RainerJ/td88ay27/

HTH
Rainer
0
 
rito1Author Commented:
Hi Rainer

This XML is failing

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
            xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
    <Application>Microsoft Macintosh Excel</Application>
    <DocSecurity>0</DocSecurity>
    <ScaleCrop>false</ScaleCrop>
    <HeadingPairs>
        <vt:vector size="2" baseType="variant">
            <vt:variant>
                <vt:lpstr>Worksheets</vt:lpstr>
            </vt:variant>
            <vt:variant>
                <vt:i4>4</vt:i4>
            </vt:variant>
        </vt:vector>
    </HeadingPairs>
    <TitlesOfParts>
        <vt:vector size="4" baseType="lpstr">
            <vt:lpstr>SheetUno</vt:lpstr>
            <vt:lpstr>SheetTwo</vt:lpstr>
            <vt:lpstr>AnotherSheet</vt:lpstr>
            <vt:lpstr>fourth sheet</vt:lpstr>
        </vt:vector>
    </TitlesOfParts>
    <LinksUpToDate>false</LinksUpToDate>
    <SharedDoc>false</SharedDoc>
    <HyperlinksChanged>false</HyperlinksChanged>
    <AppVersion>14.0300</AppVersion>
</Properties>

Open in new window


Can you see why?... is it because within 1 XML tag there are multiple colons?

Thanks,

Rit
0
 
rito1Author Commented:
Squashed for convenience here...

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><Application>Microsoft Macintosh Excel</Application><DocSecurity>0</DocSecurity><ScaleCrop>false</ScaleCrop><HeadingPairs><vt:vector size="2" baseType="variant"><vt:variant><vt:lpstr>Worksheets</vt:lpstr></vt:variant><vt:variant><vt:i4>4</vt:i4></vt:variant></vt:vector></HeadingPairs><TitlesOfParts><vt:vector size="4" baseType="lpstr"><vt:lpstr>SheetUno</vt:lpstr><vt:lpstr>SheetTwo</vt:lpstr><vt:lpstr>AnotherSheet</vt:lpstr><vt:lpstr>fourth sheet</vt:lpstr></vt:vector></TitlesOfParts><LinksUpToDate>false</LinksUpToDate><SharedDoc>false</SharedDoc><HyperlinksChanged>false</HyperlinksChanged><AppVersion>14.0300</AppVersion></Properties>

Open in new window

0
 
rito1Author Commented:
<vt:vector size="2" baseType="variant">...

Open in new window


The colon in the above isn't being replaced.
0
 
Rainer JeschorCommented:
Hi,
I think I got it now. One of the elements had some extension to it.
I updated my jsFiddle:
http://jsfiddle.net/EE_RainerJ/rb3q9cc4/

The major change are the new regex:
var regexTagOpen = /(<\w+)(:)([\w\s="]+>)/gi;
var regexTagClose = /(<\/\w+)(:)(\w+>)/gi;

Open in new window

HTH
Rainer
0
 
rito1Author Commented:
Hi Rainer,

I feel bad as I have a bunch of XML files which I am testing your pattern against and I have come up with the following:

<dcterms:modified xsi:type="dcterms:W3CDTF">2015-01-25T19:57:47Z</dctermsmodified>

Open in new window


... This is failing at the moment.
0
 
rito1Author Commented:
Hi Rainer,

I am still struggling to parse the XML. If you don't mind I would like to close this question and award you the points and re-open a new/similar question but making the changes to the JSON instead as this should be simple to parse.

I hope that makes sense.

Thank you for your support, its much appreciated indeed.

Rit
0
 
rito1Author Commented:
Really supportive and skilled particularly with Regexp.
0
 
Rainer JeschorCommented:
Hi Rit,
no problem - thanks a lot. It is always good to dig into Regex on a regular base - so powerful but so complex :-)
0
 
rito1Author Commented:
Hi Rainer,

If you have any time, you may be able to assist me with this question :-)...

http://mobile.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28605075.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.