Salt Hareket
Salt Hareket

Reputation: 764

parse a svg xml with asp

I need to get an attribute from svg file on classic asp. But my code don't work. Any idea?

id attribute from font tag.

<font id="LetterGothicStdRegular" horiz-adv-x="1228" >

asp function

function getFontId(url)
Set objXml = Server.CreateObject("MSXML2.DomDocument.6.0")
'objXml.async = False
objXml.LoadXML(url)

For Each oNode In objXml.SelectNodes("svg/defs/font")
  sKey = oNode.GetAttribute("id")
 ' sValue = oNode.Text
  getXML=sKey
Next

Set objXml= Nothing
end function

svg code:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg">
    <metadata>This is a custom SVG webfont generated by Font Squirrel.        Copyright   :  1987 1990 1991 1995 1998 2001 2002 Adobe Systems Incorporated All rights reserved        Foundry     : Adobe </metadata>
    <defs>
        <font id="LetterGothicStdRegular" horiz-adv-x="1228">
            <font-face units-per-em="2048" ascent="1520" descent="-528"/>
            <missing-glyph horiz-adv-x="500"/>
            <glyph unicode="2" horiz-adv-x="1783" d="M149 1099l66 51l672 -696l671 696l66 -51l-737 -776z"/>
            <glyph unicode="8" horiz-adv-x="1783" d="M167 430l737 776l738 -776l-66 -51l-672 696l-671 -696z"/>
            <glyph unicode="&#x3c;" d="M201 737l776 738l51 -66l-696 -672l696 -671l-51 -66z"/>
            <glyph unicode="&#x3e;" d="M288 66l696 671l-696 672l51 66l776 -738l-776 -737z"/>
            <glyph unicode="&#x2000;" horiz-adv-x="737"/>
            <glyph unicode="&#x2001;" horiz-adv-x="1475"/>
            <glyph unicode="&#x2002;" horiz-adv-x="737"/>
            <glyph unicode="&#x2003;" horiz-adv-x="1475"/>
            <glyph unicode="&#x2004;" horiz-adv-x="491"/>
            <glyph unicode="&#x2005;" horiz-adv-x="368"/>
            <glyph unicode="&#x2006;" horiz-adv-x="245"/>
            <glyph unicode="&#x2007;" horiz-adv-x="245"/>
            <glyph unicode="&#x2008;" horiz-adv-x="184"/>
            <glyph unicode="&#x2009;" horiz-adv-x="295"/>
            <glyph unicode="&#x200a;" horiz-adv-x="81"/>
            <glyph unicode="&#x202f;" horiz-adv-x="295"/>
            <glyph unicode="&#x205f;" horiz-adv-x="368"/>
            <glyph unicode="&#xe000;" horiz-adv-x="500" d="M0 0v0v0v0v0z"/>
        </font>
    </defs>
</svg>

Upvotes: 4

Views: 866

Answers (2)

Kul-Tigin
Kul-Tigin

Reputation: 16950

You can parse specifying svg selection namespace.
Plus, the parameter name is url, this make sense if it's a url? Because loadXML loads only from a string containing xml. You should use Load with setting ServerHTTPRequest property to True if url is remote. Check out validateOnParse and resolveExternals. Here's an example:

Function getFontId(url)
    With Server.CreateObject("MSXML2.DomDocument.6.0")
        .async = False
        .validateOnParse = False 'parse only for well-formed xml, no more
        .resolveExternals = False
        .setProperty "ServerHTTPRequest", True
        .setProperty "ProhibitDTD", False ' it's true by default in 6.0
        If .Load(url) Then
            .setProperty "SelectionNamespaces", "xmlns:svg='http://www.w3.org/2000/svg'"
            Dim domAttrId
            Set domAttrId = .selectSingleNode("//svg:defs/svg:font/@id")
            If domAttrId Is Nothing Then 
                'node nor attribute not exists
            Else
                getFontId = domAttrId.Value
            End If
        Else
            With .parseError
                Err.Raise .errorCode, .srcText, .reason
            End With
        End If
    End With
End Function

Upvotes: 5

Erik Oosterwaal
Erik Oosterwaal

Reputation: 4384

Can you try something like this, below your .LoadXML() statement? (untested):

Dim root, xpath, xmlnode, font
Set root = objXml.documentElement
    xpath = "/defs/font"
    set xmlNode = xml.selectNodes(xpath)
        font = xmlNode(0).getAttribute("id")
    set xmlNode = Nothing
Set root = Nothing

There are a few things I noticed:

  • you have no root set (you can try setting it and response.writing it to see if it returns the svg node)
  • xmlNode(0) returns the first node it finds, which might be nessecary

Try response.writing different Nodes using .xml or .xmltext to see if you are in the correct part of the DOM tree.

Upvotes: 0

Related Questions