[xml] Finding a specific element and its text value in an XML message



Hello
I have a question. I am a first time libxml2 user. I am looking into using libxml2 to interpret incoming XML messages (whose XSD files I have) that I receive in a buffer from a server using TCP/IP connectionI noticed that libxml2 offers API that could help look for a certain element (by seraching for an xmlNode with xmlTextReaderNodeType() = 1), get and check its name (using xmlTextReaderConstName()) and if it is the right name (element I am looking for) get itx text value (by seraching for an xmlNode with xmlTextReaderNodeType() = 3).
 
Question:
Is this the right way to parse an XML message and get the text values embedded in the message.?
 
Thanks.
 
- Nidal Zeidat

From: xml-bounces gnome org [mailto:xml-bounces gnome org] On Behalf Of Andric, Milan
Sent: Friday, August 04, 2006 4:37 PM
To: xml gnome org
Subject: [xml] question on xml validtion and xpath

Hi,

 

I’m trying to validate my XML file against XSchema that is linked inline to such file.

XML file has elements with default namespace, without prefix, to which XSchema documents links to, enabling it to do its validation work.

 

Validation goes ok. Next thing I tried is using Xpath _expression_ to select something using absolute path from my XML file and XPath returned no results.

 

Sample XML file:

 

<?xml version="1.0" encoding="utf-8" ?>

<TU xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com config.xsd"> 

            <Model Name="Models">

                       

            </Model>                   

</TU>

 

Associated XML Schema:

 

<?xml version="1.0" ?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com">

            <xs:element name="TU">

            // validation rules here…

</xs:element>

</xs:schema>

 

 

Question: My Xpath Inquiry:

 

/TU/Model

 

returns no results – why is this and how can I make it so that both Xpath would correctly return such node selection, as well as XSchema would be able to validate the same document?

 

If I define XML file with namespace prefix:

 

<?xml version="1.0" encoding="utf-8" ?>

<TU xmlns:MyNS="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com config.xsd"> 

            <Model Name="Models">

                       

            </Model>                   

</TU>

 

Then XPath selection works fine, but my validation fails – how can I make this work?

 

Any help would be very appreciated.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]