Project

Profile

Help

What does this do

Added by Keith Miner almost 5 years ago

I noticed that the following line appears in most of the python/saxonc example files:

xdmAtomicval = proc.make_boolean_value(False)

Wondering what it does (because once declared it doesn't appear to be used) I checked out the documentation when reads:

make_boolean_value(...) make_boolean_value(self, value) Factory method: makes a XdmAtomicValue representing a boolean Value

Args: value (boolean): True or False, to determine which boolean value is required

Returns: PyAtomicValue: The corresponding XdmValue

While I'm sure that is a completely accurate statement it doesn't tell me anything. What does it do? Why is it in those examples? (Those files run find when I comment out that line.) Why would I want to use it in the future?

Thanks!


Replies (7)

Please register to reply

RE: What does this do - Added by Martin Honnen almost 5 years ago

If you want to pass a boolean value (i.e. True or False) as a parameter from Python to XSLT or XQuery or XPath processing you could use that function to create a PyAtomicValue from the Python boolean value so that the Saxon APIs have the corresponding type they can handle. It is just a conversion from the Python type system to the XDM type system representation Saxon uses.

RE: What does this do - Added by O'Neil Delpratt almost 5 years ago

Martin has correctly answered your question, which I hope clears up its purpose. I believe you are referring to the example 'test_example2.py'. I will fix the example by removing the boolean PyXdmAtomic value as it is not used in the example.

If you look at the pyunit tests in the file 'tets_saxonc.py' you will see an example which make use of the make_boolean_value:

def testReusability(saxonproc):
    queryproc = saxonproc.new_xquery_processor()
    queryproc.clear_properties()
    queryproc.clear_parameters()

    input_ =  saxonproc.parse_xml(xml_text="<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>")
    queryproc.declare_namespace("", "http://one.uri/")
    queryproc.set_query_content("declare variable $p as xs:boolean external; exists(/foo) = $p")

    queryproc.set_context(xdm_item=input_)

    value1 = saxonproc.make_boolean_value(True)
    queryproc.set_parameter("p",value1)
    result = queryproc.run_query_to_value()
       
    assert result is not None
    assert result.is_atomic
    assert result.boolean_value

    queryproc.clear_parameters()
    queryproc.clear_properties()    
    
    queryproc.declare_namespace("", "http://two.uri")
    queryproc.set_query_content("declare variable $p as xs:integer external; /*/bar + $p")
    
    queryproc.set_context(xdm_item=input_)

    value2 = saxonproc.make_long_value(6)
    queryproc.set_parameter("p",value2)
        
    result2 = queryproc.run_query_to_value()
    assert result2.integer_value == 18

In the example you will see the variable result which is an XdmAtomicValue object, it is also of Xdm type boolean. You can unwrap the XdmAtomicValue to the Python primitive type boolean by calling the class property name boolean_value.

RE: What does this do - Added by Keith Miner almost 5 years ago

Martin & O'Neil,

Thanks for the quick and helpful replies! You guys are great.

Clearly I have a lot to learn here. I refer to the documentation on this site as I read through the provided example files. Are there other resources you recommend for learning saxonc and using it in Python?

Thanks again

RE: What does this do - Added by O'Neil Delpratt almost 5 years ago

Hi Keith,

In answering your question it really depends on where you are starting from and how much XSLT, XPath and XQuery you know already? In fact I just noticed that the example of using the XDM types I gave you above is for use with XQuery instead of in XSLT. We could easily write something similar using XSLT and the XDM types.

Because it is still early days with Saxon/C and python there is not that much material available online. If you have specific questions we would be happy to answer and and help you through.

For Saxon/C the API documentation is the best way at the moment get to grips with software. The Saxon/C API is really a wrapper of the C++ API and goes at great lengths to mimic the Saxon API on Java.

RE: What does this do - Added by Keith Miner almost 5 years ago

Hi O'Neil,

I have been using XSLT/XPath fairly regularly for 4 year, most intensely over the last year. So perhaps an intermediate level user. I'm definitely a rank beginner in Python, although I have written in other languages (Perl, JavaScript, etc.)

How early days are we with Saxon/C and python?

Thanks again

RE: What does this do - Added by Michael Kay almost 5 years ago

How early days are we with Saxon/C and python?

Well, the XSLT processor code itself is very mature and stable; it's the same code as the Java product. What's new is the Python (and C) language bindings, installation and deployment. So we're seeing a few issues relating to installation of the software (because we haven't tested that on each one of the hundreds of different platform configurations out there), API usability issues, documentation, and operational issues connected with things such as releasing of memory. With the great feedback we're getting from early users of the product, we hope to rapidly iron those issues out.

RE: What does this do - Added by Keith Miner almost 5 years ago

Thanks for the update Michael. Our use of this in production is still several months off. I'm just trying to learn it (and Python) in preparation for work we will do next year.

    (1-7/7)

    Please register to reply