java - XPath XML namespace Issue -


i have read xml has prefixes namespaces not defined

example

<department:employee xsi:schema="www.abcd.com abcd.xsd" > <emp:name> john </emp:name>  </department:employee> 

i using xpath in java

is possible create dynamic namespace , bind document object

regards

if want use dom api read input, you'll have fix first.

regular expressions can help. write function following:

  • read file text. make sure use correct character encoding.
  • regex-search "xmlns:([^\\s:]+)=" find declared prefixes, store them in map.
  • regex-search "<([^\\s:]+):" find used namespace prefixes.
  • determine prefixes used, not declared: every used prefix find if it's in map.
  • for each remaining prefix, replace first "[^\\s?-]>" in document

    " xmlns:" + prefix + "='http://tempuri.org/" + prefix + "'$0" 
  • try toad resulting string dom api, return document.
  • the namespace uri department should http://tempuri.org/department, etc.

the regex patterns approximations. rules allowed characters in xml namespace prefixes a bit more complicated that. [^\s:]+ not real deal, might enough situation.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -