Re: [xml] [xpath] How to print xml fragment



Hi,

bagnacauda wrote:
My input xml file has got a recurring structure "TransactionError" as
follows:
 
......
  <TransactionErrorRecords>
   <TransactionError>
    <TransactionErrorReference>bbb</TransactionErrorReference>
    <TransactionErrorMessage>ccc</TransactionErrorMessage>
   </TransactionError>
   <TransactionError>
    <TransactionErrorReference>ddd</TransactionErrorReference>
    <TransactionErrorMessage>eee</TransactionErrorMessage>
   </TransactionError>
.......
  </TransactionErrorRecords>
 
I need to extract the content of the xml file (as a string) between each
couple <TransactionError>,</TransactionError>, the two tags included.
So I basically need to be able to populate a string in a loop with
   <TransactionError>
    <TransactionErrorReference>bbb</TransactionErrorReference>
    <TransactionErrorMessage>ccc</TransactionErrorMessage>
   </TransactionError>
 
then with
 
   <TransactionError>
    <TransactionErrorReference>ddd</TransactionErrorReference>
    <TransactionErrorMessage>eee</TransactionErrorMessage>
   </TransactionError>
 
and so on
 
I can identify the nodeset (all TransactionError's) with an xpath
expression but then how can I get the string value? I've tried to use
the xpath2.c example as a guide by printing cur->content when
cur=TransactionError but it won't work.

As noted before, XSLT might be a good option here, depending on what the rest
of your algorithm does.

To write a node to a string, you can use xmlNodeDump or xmlNodeDumpOutput (if
you need encoding) to serialise it to a buffer:

http://xmlsoft.org/html/libxml-tree.html#xmlBufferCreate
http://xmlsoft.org/html/libxml-tree.html#xmlNodeDump
http://xmlsoft.org/html/libxml-tree.html#xmlBuffer
http://xmlsoft.org/html/libxml-tree.html#xmlOutputBuffer

Stefan



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