1

I've decoded a string in Base64 with xml2js library and i got this value :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="293" height="102"
 viewBox="0 0 293 102"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<image 
width="293" 
height="102" 
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=" />
</svg>

i cut the encode value because it's to long

I'm blocked for getting the value of xlink:href, is there any technics or library for getting the attribute ?

I mean i want to get this value only : data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=

Thank's

3
  • You could try using a regex to match only that part of the string. Something like /xlink:href="([^"]*)"/ Commented Nov 15, 2018 at 15:46
  • 1
    It's a svg you can parse it using DOMParser Commented Nov 15, 2018 at 15:59
  • @JEY that's probably the most robust solution :) Commented Nov 15, 2018 at 16:03

1 Answer 1

2

You can use DOMParser to parse the xml string. Then you can operate on the resulting Document as usual:

const xmlString = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="293" height="102"
 viewBox="0 0 293 102"
 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  version="1.2" baseProfile="tiny">
<image 
width="293" 
height="102" 
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANS......pASYAAAAASUVORK5CYII=" />
</svg>`;

const domParser = new DOMParser();
const xmlDoc = domParser.parseFromString(xmlString, 'application/xml');
const imageElement = xmlDoc.getElementsByTagName('image')[0];
const hrefAttr = imageElement.getAttribute('xlink:href');
console.log(hrefAttr);

Sign up to request clarification or add additional context in comments.

2 Comments

just use querySelector and getAttributeNS like i did stackblitz.com/edit/typescript-yx3fyn
I've use this : var urlImage = xml.match(/xlink:href="([^"]*)"/); var finalUrl = urlImage[1] and it works Thank's @Alex K and Jey for your solutions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.