/// <summary> /// Search for the prefix associated with specified namespace URI. /// </summary> /// <param name="element">the element to start searching from</param> /// <param name="ns">the namespace prefix</param> /// <returns>the prefix bound to the namespace URI; or null if there is no such namespace</returns> public static String LookupPrefix(DomElement element, String ns) { Dictionary <String, DomAttr> attributes = element.getAttributesMap(); foreach (KeyValuePair <String, DomAttr> entry in attributes) { String name = entry.Key; DomAttr value = entry.Value; if (name.StartsWith("xmlns:") && String.Equals(value.getValue(), ns)) { return(name.Substring(6)); } } foreach (DomNode child in element.getChildren()) { if (child is DomElement) { String prefix = LookupPrefix((DomElement)child, ns); if (prefix != null) { return(prefix); } } } return(null); }
private static DomNode CreateFrom(SgmlPage page, XmlNode source, bool handleXHTMLAsHTML) { if (source.NodeType == XmlNodeType.Text) { return(new DomText(page, source.Value)); } if (source.NodeType == XmlNodeType.ProcessingInstruction) { return(new DomProcessingInstruction(page, source.Name, source.Value)); } if (source.NodeType == XmlNodeType.Comment) { return(new DomComment(page, source.Value)); } if (source.NodeType == XmlNodeType.DocumentType) { XmlDocumentType documentType = (XmlDocumentType)source; return(new DomDocumentType(page, documentType.Name, documentType.PublicId, documentType.SystemId)); } String ns = source.NamespaceURI; String localName = source.LocalName; if (handleXHTMLAsHTML && String.Equals(HTMLParser.XHTML_NAMESPACE, ns)) { ElementFactory factory = HTMLParser.getFactory(localName); return(factory.createElementNS(page, ns, localName, source.Attributes)); } XmlAttributeCollection nodeAttributes = source.Attributes; if (page != null && page.isHtmlPage()) { localName = localName.ToUpper(); // TODO : Locale.ENGLISH } String qualifiedName; if (source.Prefix == null) { qualifiedName = localName; } else { qualifiedName = source.Prefix + ':' + localName; } String namespaceURI = source.NamespaceURI; if (String.Equals(HTMLParser.SVG_NAMESPACE, namespaceURI)) { return(HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, nodeAttributes)); } Dictionary <String, DomAttr> attributes = new Dictionary <string, DomAttr>(); for (int i = 0; i < nodeAttributes.Count; i++) { XmlAttribute attribute = (XmlAttribute)nodeAttributes.Item(i); String attributeNamespaceURI = attribute.NamespaceURI; String attributeQualifiedName; if (attribute.Prefix != null) { attributeQualifiedName = attribute.Prefix + ':' + attribute.LocalName; } else { attributeQualifiedName = attribute.LocalName; } String value = attribute.Value; bool specified = attribute.Specified; DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.Add(attribute.Name, xmlAttribute); } return(new DomElement(namespaceURI, qualifiedName, page, attributes)); }
private static DomNode CreateFrom(SgmlPage page, XmlNode source, bool handleXHTMLAsHTML) { if (source.NodeType == XmlNodeType.Text) { return new DomText(page, source.Value); } if (source.NodeType == XmlNodeType.ProcessingInstruction) { return new DomProcessingInstruction(page, source.Name, source.Value); } if (source.NodeType == XmlNodeType.Comment) { return new DomComment(page, source.Value); } if (source.NodeType == XmlNodeType.DocumentType) { XmlDocumentType documentType = (XmlDocumentType)source; return new DomDocumentType(page, documentType.Name, documentType.PublicId, documentType.SystemId); } String ns = source.NamespaceURI; String localName = source.LocalName; if (handleXHTMLAsHTML && String.Equals(HTMLParser.XHTML_NAMESPACE, ns)) { ElementFactory factory = HTMLParser.getFactory(localName); return factory.createElementNS(page, ns, localName, source.Attributes); } XmlAttributeCollection nodeAttributes = source.Attributes; if (page != null && page.isHtmlPage()) { localName = localName.ToUpper(); // TODO : Locale.ENGLISH } String qualifiedName; if (source.Prefix == null) { qualifiedName = localName; } else { qualifiedName = source.Prefix + ':' + localName; } String namespaceURI = source.NamespaceURI; if (String.Equals(HTMLParser.SVG_NAMESPACE, namespaceURI)) { return HTMLParser.SVG_FACTORY.createElementNS(page, namespaceURI, qualifiedName, nodeAttributes); } Dictionary<String, DomAttr> attributes = new Dictionary<string, DomAttr>(); for (int i = 0; i < nodeAttributes.Count; i++) { XmlAttribute attribute = (XmlAttribute)nodeAttributes.Item(i); String attributeNamespaceURI = attribute.NamespaceURI; String attributeQualifiedName; if (attribute.Prefix != null) { attributeQualifiedName = attribute.Prefix + ':' + attribute.LocalName; } else { attributeQualifiedName = attribute.LocalName; } String value = attribute.Value; bool specified = attribute.Specified; DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified); attributes.Add(attribute.Name, xmlAttribute); } return new DomElement(namespaceURI, qualifiedName, page, attributes); }