How to combine field and attribute searches in XML

Last Reviewed: August 5, 2013

Article: DTS0195

Applies to: dtSearch Engine 6

dtSearch indexes attributes in XML files as if they were fields.   For example: 

<name first="John" last="Smith" /> 

This is indexed as it if were: 





In some cases, though, the intent of an attribute is to modify the field name, not to provide a value.   Example:

<name type="first">John</name>

<name type="last">Smith</name>

The default treatment of XML attributes in dtSearch makes it difficult to search for a first name that was stored with this use of attributes.  (A search for "(/name/type contains first) and (/name contains John)" will match any document that contains a "first" value in the "/name/type" field and also "John" in a /name field.   However, these may be different /name fields in the same document.)  A simple transformation can be performed on the XML that will cause the fields to be indexed in a way that permits searching on a first name.   To do this, replace the attribute with a generated field that combines the attribute and named value, like this:





With this field structure, a search can be limited to the first name field as follows:

/name/TypeFirst contains John

See also:

Field searching

Copyright © 1995-2005 dtSearch Corp.