?
Solved

Python XPath Question

Posted on 2007-10-18
4
Medium Priority
?
316 Views
Last Modified: 2008-01-09
I have a peice of XML that I need to parse and extract all message text of type "Fatal".  I am trying to use 4Suite XML library and XPath.  Below is a sample of the XML

      <Message type="Info" category="Debug">Remote User:JBOYD</Message>
      <Message type="Info" category="Debug">Standard Login</Message>
      <Message type="Info" category="Server">Worksheet Saved</Message>
      <Message type="Info" category="Audit">WORKSHEET AUDIT STARTED, PLEASE WAIT..</Message>
      <Message type="Warning" category="Audit" fieldID="70" MISMORef="//LOAN/_APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment () Does NOT Match default ($1,100.65)</Message>
      <Message type="Warning" category="Audit" fieldID="70" MISMORef="//LOAN/APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment may be Incorrect (Too Low)</Message>
      <Message type="Warning" category="Audit" fieldID="9" MISMORef="//LOAN/_CLOSING_DOCUMENTS/LOAN_DETAILS/DocumentPreparationDate">Document Date is more than '5' days in the past</Message>
      <Message type="Warning" category="Audit" fieldID="127" MISMORef="//LOAN/_APPLICATION/MI_DATA/MI_RENEWAL_PREMIUM/@_MonthlyPaymentAmount">Is mortgage insurance required? (LTV &lt; 80%)</Message>
      <Message type="Fatal" category="Audit" fieldID="70" MISMORef="//LOAN/APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment is Incorrect (Negative Amount)</Message>
      <Message type="Fatal" category="Audit" fieldID="70" MISMORef="//LOAN/_APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">No P &amp; I Payment Amount</Message>
      <Message type="Info" category="Audit">Worksheet Audit complete</Message>
      <Message type="Info" category="Server">Worksheet Audited (outstanding audits detected)</Message>
0
Comment
Question by:maytawn
4 Comments
 
LVL 15

Expert Comment

by:efn
ID: 20106507
What is your question?
0
 
LVL 1

Author Comment

by:maytawn
ID: 20106559
How do I extract all message text of type "Fatal" using an XML library (preferably the 4Suite library) in the same XML posted above.
0
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 2000 total points
ID: 20106780
Try the following XPath filter
//Message[@type="Fatal"]
0
 
LVL 29

Expert Comment

by:pepr
ID: 20106991
The alternative -- without using XPath:

=================================================================
s = '''<Message type="Info" category="Debug">Remote User:JBOYD</Message>
      <Message type="Info" category="Debug">Standard Login</Message>
      <Message type="Info" category="Server">Worksheet Saved</Message>
      <Message type="Info" category="Audit">WORKSHEET AUDIT STARTED, PLEASE WAIT..</Message>
      <Message type="Warning" category="Audit" fieldID="70" MISMORef="//LOAN/_APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment () Does NOT Match default ($1,100.65)</Message>
      <Message type="Warning" category="Audit" fieldID="70" MISMORef="//LOAN/APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment may be Incorrect (Too Low)</Message>
      <Message type="Warning" category="Audit" fieldID="9" MISMORef="//LOAN/_CLOSING_DOCUMENTS/LOAN_DETAILS/DocumentPreparationDate">Document Date is more than '5' days in the past</Message>
      <Message type="Warning" category="Audit" fieldID="127" MISMORef="//LOAN/_APPLICATION/MI_DATA/MI_RENEWAL_PREMIUM/@_MonthlyPaymentAmount">Is mortgage insurance required? (LTV &lt; 80%)</Message>
      <Message type="Fatal" category="Audit" fieldID="70" MISMORef="//LOAN/APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">P&amp;I Payment is Incorrect (Negative Amount)</Message>
      <Message type="Fatal" category="Audit" fieldID="70" MISMORef="//LOAN/_APPLICATION/LOAN_PRODUCT_DATA/LOAN_FEATURES/@OriginalPrincipalAndInterestPaymentAmount">No P &amp; I Payment Amount</Message>
      <Message type="Info" category="Audit">Worksheet Audit complete</Message>
      <Message type="Info" category="Server">Worksheet Audited (outstanding audits detected)</Message>'''

from xml.dom.minidom import parseString

doc = parseString('<wrap>' + s + '</wrap>')   # the document must be one node

for msg in doc.getElementsByTagName('Message'):
    if msg.getAttribute('type') == 'Fatal':
        print msg.toxml()      # or process it somehow else
=================================================================

It prints the Fatal messages as XML strings (attributes may have a different order than in the original).
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question