开发者

Does XPath will return a object other than String

I have map xml as below. I can retrieve a value using XPath but can I retrieve object instead?. For example I want Map object to be retured if I say /list/* . Is it possible to retrieve as object.

<list>
    <map>
        <val name="obj_type">USER</val>
        <val name="ret_name">user</val>
        <list name="attributes">
            <map>
    开发者_高级运维            <val name="obj_type">USER_ID</val>
                <val name="ret_name">userID</val>
            </map>
            <map>
                <val name="obj_type">
                    USER_UsernamePasswordCredential
                </val>
                <list name="attributes">
                    <map>
                        <val name="obj_type">UNP_Username</val>
                        <val name="ret_name">UserName</val>
                    </map>
                    <map>
                        <val name="obj_type">UNP_Password</val>
                        <val name="ret_name">Password</val>
                    </map>
                </list>
            </map>
        </list>
    </map>
</list>


From http://www.w3.org/TR/xpath/#section-Introduction

An expression is evaluated to yield an object, which has one of the following four basic types:

  • node-set (an unordered collection of nodes without duplicates)
  • boolean (true or false)
  • number (a floating-point number)
  • string (a sequence of UCS characters)

This four basic types (plus others regarding update, order and completeness) are mapped by standar DOM XPath API. From http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html#XPathResult

Definition group XPathResultType

An integer indicating what type of result this is.

If a specific type is specified, then the result will be returned as the corresponding type, using XPath type conversions where required and possible.

Defined Constants

  • BOOLEAN_TYPE
    The result is a boolean as defined by [XPath 1.0]. Document modification does not invalidate the boolean, but may mean that reevaluation would not yield the same boolean.
  • NUMBER_TYPE
    The result is a number as defined by [XPath 1.0]. Document modification does not invalidate the number, but may mean that reevaluation would not yield the same number.
  • STRING_TYPE
    The result is a string as defined by [XPath 1.0]. Document modification does not invalidate the string, but may mean that the string no longer corresponds to the current document.
  • UNORDERED_NODE_SNAPSHOT_TYPE
    The result is a node set as defined by [XPath 1.0] that will be accessed as a snapshot list of nodes that may not be in a particular order. Document modification does not invalidate the snapshot but may mean that reevaluation would not yield the same snapshot and nodes in the snapshot may have been altered, moved, or removed from the document.


It's only an object if you bind the data to an object instance. Maybe you mean an XML element? In any case, it's dependent on your programming language.


the XPATH works only at the DOM level and return Nodes unstead of the real objects.

if you want to work with the real data types you can use some existing frameworks like castor fo java, and for dotnet you can see this article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜