Solved

Replace colon characters in XML tag attribute

Posted on 2015-01-27
15
311 Views
Last Modified: 2015-01-27
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
0
Comment
Question by:rito1
  • 9
  • 6
15 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40572651
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
 
LVL 1

Author Comment

by:rito1
ID: 40572742
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
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40572752
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
 
LVL 1

Author Comment

by:rito1
ID: 40572788
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
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40572898
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
 
LVL 1

Author Comment

by:rito1
ID: 40573101
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
 
LVL 1

Author Comment

by:rito1
ID: 40573106
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:rito1
ID: 40573128
<vt:vector size="2" baseType="variant">...

Open in new window


The colon in the above isn't being replaced.
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40573231
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
 
LVL 1

Author Comment

by:rito1
ID: 40573305
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
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 40573336
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
 
LVL 1

Author Comment

by:rito1
ID: 40573578
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
 
LVL 1

Author Closing Comment

by:rito1
ID: 40573581
Really supportive and skilled particularly with Regexp.
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 40573979
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
 
LVL 1

Author Comment

by:rito1
ID: 40574015
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now