jindw / xmldom

A PURE JS W3C Standard based(XML DOM Level2 CORE) DOMParser and XMLSerializer.
Other
816 stars 265 forks source link

Can not handle &xsd;string case in the xml #94

Open kevinprotoss opened 10 years ago

kevinprotoss commented 10 years ago

Hello,

I used this module for parsing xml file which has the following data.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE owl [
     <!ENTITY tzont  "http://www.w3.org/2006/timezone#" >
     <!ENTITY owl  "http://www.w3.org/2002/07/owl#" >
     <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >
     <!ENTITY iso "http://www.daml.org/2001/09/countries/iso#" >
     <!ENTITY us-states "http://www.daml.ri.cmu.edu/ont/USRegionState.daml#" >
     <!ENTITY iso-3166-ont "http://www.daml.org/2001/09/countries/iso-3166-ont#" >
   ]>

<rdf:RDF
  xmlns    = "http://www.w3.org/2006/timezone-world#"
  xml:base    = "http://www.w3.org/2006/timezone-world"
  xmlns:tz-world = "http://www.w3.org/2006/timezone-world#"
  xmlns:tzont = "http://www.w3.org/2006/timezone#"
  xmlns:owl = "http://www.w3.org/2002/07/owl#"
  xmlns:rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs = "http://www.w3.org/2000/01/rdf-schema#"
  xmlns:iso = "http://www.daml.org/2001/09/countries/iso#"
  xmlns:us-states = "http://www.daml.ri.cmu.edu/ont/USRegionState.daml#"
  xmlns:iso-3166-ont = "http://www.daml.org/2001/09/countries/iso-3166-ont#"
  xmlns:xsd = "http://www.w3.org/2001/XMLSchema#">

<!-- intances of time zones -->

  <tzont:TimeZone rdf:ID="YTZ">
    <tzont:name rdf:datatype="&xsd;string">Yankee Time</tzont:name>
    <tzont:name rdf:datatype="&xsd;string">International Date Line West</tzont:name>
    <tzont:GMToffset rdf:datatype="&xsd;duration">-PT12H</tzont:GMToffset>
  </tzont:TimeZone>

</rdf:RDF>

However, all these &xsd; stuff can not be parsed by xmldom. I get this error from sax.js:

entity not found: `&xsd;string`

I think the xml is correct according to xml specification.

Best regards, Kevin

jindw commented 10 years ago

see: sax.parse(source,defaultNSMap,entityMap); specify your entityMap yourself

it's new feature for xmldom: specify the entityMap on DOMParser constructor

maisk commented 10 years ago

Feature request: DOMParser option parameter entityMap.

if you replace the var entityMap = {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"} with var entityMap = options.entityMap ? options.entityMap : {'lt':'<','gt':'>','amp':'&','quot':'"','apos':"'"} in source file dom-parser.js at method: DOMParser.prototype.parseFromString = function(source,mimeType)

i think is a good solution.

codler commented 7 years ago

A "options.entityMap" patch would be great! #131

xZGit commented 4 years ago

you can try this:

const XMLReader  = require('xmldome/sax').XMLReader
const oldParse = XMLReader.prototype.parse
const myEntityMap = {
    'times': '*'
};
XMLReader.prototype.parse =  function(source,defaultNSMap,entityMap){
   return oldParse.call(this, source, defaultNSMap, Object.assign(entityMap, myEntityMap));
};