How to select a sibling's child node using XPath in Python? - python

I'm using Python and lxml. My xml file:
<Example>
<Path>
<Some.Node>
// ...
</Some.Node>
<Some.Node>
<Known.Node KnownAttribute="123"/>
<Some.Stuff>
<Nothing.Important>Bla</Nothing.Important>
</Some.Stuff>
<Relevant.Node>
<Property>
<Name>Some</Name>
<Value>True</Value>
</Property>
<Property>
<Name>Known.Name</Name>
<Value>Desired Value</Value>
</Property>
<Property>
<Name>Some.Other</Name>
<Value>Yes</Value>
</Property>
// ...
</Relevant.Node>
// ...
</Some.Node>
<Some.Node>
// ...
</Some.Node>
</Path>
</Example>
There are multiple <Some.Node> nodes and I'm only interested in the one with KnownAttribute equal to 123. This part I got:
query = "//Known.Node[#KnownAttribute='%s']" % attribute_value
However, I need to get the value of <Relevant.Node>/<Property>/<Value> where <Name> has value Known.Name.
This was my best try but it didn't work:
root = etree.parse(xml_file).getroot()
query = "//Known.Node[#KnownAttribute='%s']/..//Property[Name='Known.Name']/Value" % attribute_value
result = root.xpath(query)
print(result[0].text)
It should print, of course, Desired Value but it just returns empty value/whitespace.
How can I get the value I need?

You are really close. You can ask for the text of the node in the xpath expression.
query = "//Known.Node[#KnownAttribute='%s']/..//" % attribute_value
query += "Property[Name='Known.Name']/Value/text()"
result = root.xpath(query)
print(result[0])
# prints:
Desired Value

Related

Schema validation failed; XML does not comply with UBL 2.1 standards in line with ZATCA specifications

I am trying to use validate my xml with UBL 2.1 standards in line with Saudi Arabia ZATCA(Zakat, Tax and Customs Authority) specifications.The url of this validatoe : https://sandbox.zatca.gov.sa/TestXML .But I can't validate that as my xml looking great but I don't understand what's going wrong .I used python json2xml package for creating xml.This package generate xml from json.
Erros list what I am getting from ZATCA XML Validator:
category : XSD_SCHEMA_ERROR
code :SAXParseException
message : Schema validation failed; XML does not comply with UBL 2.1 standards in line with ZATCA specifications
Here is my Xml code:
<?xml version="1.0" ?>
<Invoice>
<ProfileID>reporting:1.0</ProfileID>
<ID>INV004</ID>
<UUID>fd5a7cc4-2316-49ee-ac07-6f4be4be3731</UUID>
<IssueDate>2022-08-13</IssueDate>
<IssueTime>23:46:07</IssueTime>
<InvoiceTypeCode>388</InvoiceTypeCode>
<InvoiceTypeCodeName>0101001</InvoiceTypeCodeName>
<DocumentCurrencyCode>SAR</DocumentCurrencyCode>
<TaxCurrencyCode>SAR</TaxCurrencyCode>
<Note/>
<OrderReference>
<ID/>
</OrderReference>
<ContractDocumentReference>
<ID/>
</ContractDocumentReference>
<AdditionalDocumentReference>
<UUID>4</UUID>
<PIH>
<Attachment>
<EmbeddedDocumentBinaryObject>ET05jV7roub7D66wOAQ49TQ8mCkyldhmH7B8CV3Rc6g=</EmbeddedDocumentBinaryObject>
</Attachment>
</PIH>
<QR>
<Attachment>
<EmbeddedDocumentBinaryObject>5D6ZU7f6nb+s1szmMw46l4NZ7yTy0p1wi0ZUMsdQWBE=</EmbeddedDocumentBinaryObject>
</Attachment>
</QR>
</AdditionalDocumentReference>
<Signature>
<ID>urn:oasis:names:specification: ubl:signature:Invoice</ID>
<SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped: xades</SignatureMethod>
</Signature>
<AccountingSupplierParty>
<Party>
<PartyLegalEntity>
<RegistrationName>Altaf Miazee</RegistrationName>
</PartyLegalEntity>
<PartyIdentification>
<ID/>
</PartyIdentification>
<PartyTaxScheme>
<CompanyID>300600363600003</CompanyID>
</PartyTaxScheme>
<PostalAddress>
<Country>
<IdentificationCode>BD</IdentificationCode>
</Country>
<AdditionalStreetName>Altafbari</AdditionalStreetName>
<StreetName>dhaka</StreetName>
<BuildingNumber>1233</BuildingNumber>
<PlotIdentification>1233</PlotIdentification>
<CityName>Dhaka</CityName>
<PostalZone>12302</PostalZone>
<CountrySubentity>Dhaka</CountrySubentity>
<CitySubdivisionName>miazee</CitySubdivisionName>
</PostalAddress>
</Party>
</AccountingSupplierParty>
<AccountingCustomerParty>
<Party>
<PartyLegalEntity>
<RegistrationName>Hosen MD Altaf</RegistrationName>
</PartyLegalEntity>
<PartyIdentification>
<ID>398765409876333</ID>
</PartyIdentification>
<PartyTaxScheme>
<CompanyID>398765409876333</CompanyID>
</PartyTaxScheme>
<PostalAddress>
<StreetName>الملك سلمان</StreetName>
<AdditionalStreetName>الملك سلمان</AdditionalStreetName>
<BuildingNumber>1234</BuildingNumber>
<PlotIdentification>1234</PlotIdentification>
<CityName>dhaka</CityName>
<PostalZone>12234</PostalZone>
<CountrySubentity>Dhaka</CountrySubentity>
<CitySubdivisionName>الملك سلمان</CitySubdivisionName>
<Country>
<IdentificationCode>BD</IdentificationCode>
</Country>
</PostalAddress>
</Party>
</AccountingCustomerParty>
<Delivery>
<ActualDeliveryDate>2022-08-25</ActualDeliveryDate>
<LatestDeliveryDate/>
</Delivery>
<PaymentMeans>
<PaymentMeansCode>10</PaymentMeansCode>
<PayeeFinancialAccount>
<PaymentNote/>
</PayeeFinancialAccount>
</PaymentMeans>
<AllowanceCharge>
<TaxCategory>
<ID>S</ID>
<Percent>0.0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
<ChargeIndicator>False</ChargeIndicator>
<MultiplierFactorNumeric>52.1</MultiplierFactorNumeric>
<Amount>0.00</Amount>
<AmountcurrencyID>SAR</AmountcurrencyID>
<BaseAmount>164263.68</BaseAmount>
<BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
</AllowanceCharge>
<LegalMonetaryTotal>
<LineExtensionAmount>164263.68</LineExtensionAmount>
<LineExtensionAmountCurrencyID>SAR</LineExtensionAmountCurrencyID>
<AllowanceTotalAmount>0.00</AllowanceTotalAmount>
<AllowanceTotalAmountcurrencyID>SAR</AllowanceTotalAmountcurrencyID>
<TaxExclusiveAmount>146664.00</TaxExclusiveAmount>
<TaxExclusiveAmountcurrencyID>SAR</TaxExclusiveAmountcurrencyID>
<TaxInclusiveAmount>164263.68</TaxInclusiveAmount>
<TaxInclusiveAmountcurrencyID>SAR</TaxInclusiveAmountcurrencyID>
<PrepaidAmount/>
<PrepaidAmountcurrencyID>SAR</PrepaidAmountcurrencyID>
<PayableAmount>164263.68</PayableAmount>
<PayableAmountcurrencyID>SAR</PayableAmountcurrencyID>
</LegalMonetaryTotal>
<TaxTotal>
<TaxAmount>17599.68</TaxAmount>
<VatAmountCurrency>SAR</VatAmountCurrency>
<VatAmountInAccountingCurrency>17599.68</VatAmountInAccountingCurrency>
<CurrencyForVatAmountInAccountingCurrency>SAR</CurrencyForVatAmountInAccountingCurrency>
<TaxSubtotal>
<TaxableAmount>146664.0</TaxableAmount>
<TaxableAmountcurrencyID>SAR</TaxableAmountcurrencyID>
<TaxCategory>
<ID>E</ID>
<Percent>1</Percent>
<TaxExemptionReasonCode>TYIEWE</TaxExemptionReasonCode>
<TaxExemptionReason>lksdkskak</TaxExemptionReason>
<TaxScheme>
<ID>10.12</ID>
</TaxScheme>
</TaxCategory>
<TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
</TaxSubtotal>
<TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
</TaxTotal>
<InvoiceLine>
<item>
<ID>1</ID>
<InvoicedQuantity>12</InvoicedQuantity>
<InvoicedQuantityUnitCode>nos</InvoicedQuantityUnitCode>
<LineExtensionAmount>146664.0</LineExtensionAmount>
<LineExtensionAmountcurrencyID>SAR</LineExtensionAmountcurrencyID>
<AllowanceCharge>
<ChargeIndicator>False</ChargeIndicator>
<MultiplierFactorNumeric>0.0</MultiplierFactorNumeric>
<Amount>0</Amount>
<AmountcurrencyID>SAR</AmountcurrencyID>
<BaseAmount>146664.0</BaseAmount>
<BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
</AllowanceCharge>
<TaxTotal>
<TaxAmount>17599.68</TaxAmount>
<TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
<RoundingAmount>164263.68</RoundingAmount>
<RoundingAmountcurrencyID>SAR</RoundingAmountcurrencyID>
</TaxTotal>
<Item>
<Name>altaf</Name>
<BuyersItemIdentification>
<ID/>
</BuyersItemIdentification>
<SellersItemIdentification>
<ID>ITM9</ID>
</SellersItemIdentification>
<StandardItemIdentification>
<ID/>
</StandardItemIdentification>
<ClassifiedTaxCategory>
<ID>2</ID>
<Percent>12.00</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</ClassifiedTaxCategory>
</Item>
<Price>
<PriceAmount>12222.00</PriceAmount>
<PriceAmountschemeID>SAR</PriceAmountschemeID>
<AllowanceCharge>
<ChargeIdicator>False</ChargeIdicator>
<Amount>0</Amount>
<AmountcurrencyID>SAR</AmountcurrencyID>
<BaseAmount>164263.68</BaseAmount>
<BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
</AllowanceCharge>
<BaseQuantity>12</BaseQuantity>
<BaseQuantityUnitCode/>
</Price>
</item>
</InvoiceLine>
</Invoice>
Here is the python code I wrote for creating XML:
def download_invoice_xml(request, invoice_type, invoice_no):
in_data = InvoiceReport.objects.filter(invoice_type=invoice_type, invoice_number=invoice_no)
for inv in in_data:
jsonData = inv.invoice_data
mode = inv.creation_mode
# Issuetime = jsonData['IssueTime']
# d1 = datetime.strptime(Issuetime, "%Y-%m-%dT%H:%M:%S")
# dt_to_string = d1.strftime('%d-%m-%Y %H:%M:%S')
# jsonData['IssueTime'] = dt_to_string
#
# Issuedate = jsonData['IssueDate']
# d2 = datetime.strptime(Issuedate, "%Y-%m-%d")
# dt_to_string2 = d1.strftime('%d-%m-%Y')
# jsonData['IssueDate'] = dt_to_string2
if jsonData.get('AllowanceCharge').get('ChargeIndicator') == 0:
jsonData['AllowanceCharge']['ChargeIndicator'] = False
for i in jsonData.get('InvoiceLine'):
if i.get('AllowanceCharge').get('ChargeIndicator') == 0:
i['AllowanceCharge']['ChargeIndicator'] = False
if i.get('Price').get('AllowanceCharge').get('ChargeIndicator') == 0:
i['Price']['AllowanceCharge']['ChargeIndicator'] = False
xml_output = json2xml.Json2xml(jsonData, wrapper="Invoice", pretty=True, attr_type=False).to_xml()
now = datetime.now()
if xml_output:
response = HttpResponse(xml_output, content_type='application/xml')
xml_date = jsonData['IssueDate'].replace("-", "")
xml_time = jsonData['IssueTime'].replace(":", "")
filename = jsonData['AccountingSupplierParty']['Party']['PartyTaxScheme']['CompanyID'] + "_" + \
xml_date + "T" + xml_time + "_" + str(jsonData['ID']) + ".xml"
# filename = jsonData['AccountingSupplierParty']['Party']['PartyIdentification']['ID'] + "_%s_%s.xml" % (
# str((now.strftime("%Y%m%d%H%M%S"))), jsonData['ID'])
content = "attachment; filename=%s" % (filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
This is part of ZATCA E-invoice system and the project code is writen with python django.
UBL specifications that aren't met generally fall into two categories:
An Attribute is required but you are not providing it.
The order of the attributes inside the element is wrong.
To solve your problem, you should inspect each element of your XML against the UBL Schema specification and find required attributes that you're not providing or attributes out of order..
Go to this site that lists the UBL Specifications:
http://www.datypic.com/sc/ubl21/ss.html
In the search box at the top, enter the name of the element followed by the word "Type" (e.g.: Invoice --> search for InvoiceType
In the element specifications, find each attribute that its relationship starts with [1 .. this attribute is mandatory, make sure you provide a value for it.
Check the order of the attribute and make sure they are added in the correct order.
Doing that will ensure that all your elements are compliant to the UBL specifications.
There are many problems in : https://sandbox.zatca.gov.sa/
As the results in coding and sharing the invoice have many errors Zakat has been warned over and over again

Iterating over XML and selecting specific element tree content

I have an XML which looks like this:
<openie>
<triple confidence="1.000">
<subject begin="0" end="1">
<text>PAF</text>
<lemma>paf</lemma>
</subject>
<relation begin="1" end="2">
<text>gets</text>
<lemma>get</lemma>
</relation>
<object begin="2" end="6">
<text>name of web site</text>
<lemma>name of web site</lemma>
</object>
</triple>
<triple confidence="1.000">
<subject begin="0" end="1">
<text>PAF</text>
<lemma>paf</lemma>
</subject>
<relation begin="1" end="2">
<text>gets</text>
<lemma>get</lemma>
</relation>
<object begin="2" end="3">
<text>name</text>
<lemma>name</lemma>
</object>
</triple>
</openie>
The element openie is nested in here: root>document>sentences>sentence>openie
And in my function I am trying to print triples which each contain subject, relation, object elements. Unfortunately, I can not get it to work, since I am unable to get into these three elements and their text element. Which part is wrong?
def get_openie():
print('OpenIE parser start...')
tree = ET.parse('./tmp/nlp_output.xml')
root = tree.getroot()
for triple in root.findall('./document/sentences/sentence/openie/triple'):
t_subject = triple.find('subject/text').text
t_relation = triple.find('relation/text').text
t_object = triple.get('object/text').text
print(t_subject,t_relation,t_object)
Output for two triples should look like this:
PAF gets name of web site
PAF gets name
To get your t_object you're running triple.get() instead of triple.find(). Changing that fixes your problem.
def get_openie():
print('OpenIE parser start...')
tree = ET.parse('./tmp/nlp_output.xml')
root = tree.getroot()
for triple in root.findall('./document/sentences/sentence/openie/triple'):
t_subject = triple.find('subject/text').text
t_relation = triple.find('relation/text').text
t_object = triple.find('object/text').text
print(t_subject,t_relation,t_object)

Can't figure out why script isn't going any further

I have this function called zms_add_bridge that calls a function called xmlbuilder. xmlbuilder creates a global variable called xml and populates it with stuff.
When zms_add_bridge is called, it prints "here" to say that it's gotten at least that far in the script. Then, it calls xmlbuilder, which prints "xml1" along with the xml output to say that it's at least gotten that far in the script.
The issue is that once xmlbuilder exits and returns to zms_add_bridge, the script seems to die. xml is a global variable, so it should print "yes", but it's not even printing "no".
I even tried to return xml at the end of xmlbuilder to see if that would work, but it didn't (and even still, that'd be redundant since xml was declared globally, wouldn't it?)
Any clues? I'm lost.
def xmlbuilder(requestType,xbra):
global xmlbuild
global xmlbuild__request
global xmlbuild__requestElement
global xmlbuild__requestElementAttribute
global xml
xmlbuild = etree.Element("OSS", attrib={"{"+xsi+"}schemaLocation":schema}, nsmap={'xsi':xsi, None:xmlns})
xmlbuild__request = etree.SubElement(xmlbuild, "Request")
etree.SubElement(xmlbuild__request, "RequestType").text = requestType
etree.SubElement(xmlbuild__request, "RequestMode").text = "online"
etree.SubElement(xmlbuild__request, "SessionID").text = session_id
etree.SubElement(xmlbuild__request, "operName").text = apiUser
etree.SubElement(xmlbuild__request, "Version").text = version
etree.SubElement(xmlbuild__request, "Overwrite").text = "false"
xmlbuild__requestElement = etree.SubElement(xmlbuild, "RequestElement")
for index, data in xbra.items():
for key in data:
xmlbuild__requestElementAttribute = etree.SubElement(xmlbuild__requestElement, "Attribute")
etree.SubElement(xmlbuild__requestElementAttribute, "Name").text = "%s" % key
etree.SubElement(xmlbuild__requestElementAttribute, "Value").text = "%s" % data[key]
xml = etree.tostring(xmlbuild, encoding="UTF-8", pretty_print=True, xml_declaration=True)
if debug >= 1:
print "\nDEBUG: Generated XML for transmission\n"
print xml
print "xml1: " + xml
def zms_add_bridge(fsan,vlanId,maxUnicast,secure):
if debug >= 3:
print "\nDEBUG: Function: zms_add_bridge\n"
print "\nDEBUG: Get GPON parameters of ONT\n"
xbra = defaultdict(list)
xbra[1] = {'Filter_Type': 'GponOnuPhysical'}
xbra[2] = {'Filter_Condition': 'Device_Id=' + device_Handle_Id}
xbra[3] = {'Filter_Condition': 'serialNoVendorId=ZNTS'}
xbra[4] = {'Filter_Condition': 'serialNoVendorSpecificHex=' + fsan}
if debug >= 4:
print "\nDEBUG: Dictionary\n"
for index, data in xbra.items():
for key in data:
print "Index: %s: Key: %s, Value: %s" % (index, key,data[key])
print "here"
xmlbuilder("list",xbra)
if xml:
print "yes"
else:
print "no"
Output:
here
xml1: <?xml version='1.0' encoding='UTF-8'?>
<OSS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zhone.com/OSSXML" xsi:schemaLocation="http://www.zhone.com/OSSXML ossxml.xsd">
<Request>
<RequestType>list</RequestType>
<RequestMode>online</RequestMode>
<SessionID>0.379081153249641</SessionID>
<operName>boss</operName>
<Version>2.5.1</Version>
<Overwrite>false</Overwrite>
</Request>
<RequestElement>
<Attribute>
<Name>Filter_Type</Name>
<Value>GponOnuPhysical</Value>
</Attribute>
<Attribute>
<Name>Filter_Condition</Name>
<Value>Device_Id=2</Value>
</Attribute>
<Attribute>
<Name>Filter_Condition</Name>
<Value>serialNoVendorId=ZNTS</Value>
</Attribute>
<Attribute>
<Name>Filter_Condition</Name>
<Value>serialNoVendorSpecificHex=03739175</Value>
</Attribute>
</RequestElement>
</OSS>

Dictionary key and value flipping themselves unexpectedly

I am running python 3.5, and I've defined a function that creates XML SubElements and adds them under another element. The attributes are in a dictionary, but for some reason the dictionary keys and values will sometimes flip when I execute the script.
Here is a snippet of kind of what I have (the code is broken into many functions so I combined it here)
import xml.etree.ElementTree as ElementTree
def AddSubElement(parent, tag, text='', attributes = None):
XMLelement = ElementTree.SubElement(parent, tag)
XMLelement.text = text
if attributes != None:
for key, value in attributes:
XMLelement.set(key, value)
print("attributes =",attributes)
return XMLelement
descriptionTags = ([('xmlns:g' , 'http://base.google.com/ns/1.0')])
XMLroot = ElementTree.Element('rss')
XMLroot.set('version', '2.0')
XMLchannel = ElementTree.SubElement(XMLroot,'channel')
AddSubElement(XMLchannel,'g:description', 'sporting goods', attributes=descriptionTags )
AddSubElement(XMLchannel,'link', 'http://'+ domain +'/')
XMLitem = AddSubElement(XMLchannel,'item')
AddSubElement(XMLitem, 'g:brand', Product['ProductManufacturer'], attributes=bindingParam)
AddSubElement(XMLitem, 'g:description', Product['ProductDescriptionShort'], attributes=bindingParam)
AddSubElement(XMLitem, 'g:price', Product['ProductPrice'] + ' USD', attributes=bindingParam)
The key and value does get switched! Because I'll see this in the console sometimes:
attributes = [{'xmlns:g', 'http://base.google.com/ns/1.0'}]
attributes = [{'http://base.google.com/ns/1.0', 'xmlns:g'}]
attributes = [{'http://base.google.com/ns/1.0', 'xmlns:g'}]
...
And here is the xml string that sometimes comes out:
<rss version="2.0">
<channel>
<title>example.com</title>
<g:description xmlns:g="http://base.google.com/ns/1.0">sporting goods</g:description>
<link>http://www.example.com/</link>
<item>
<g:id http://base.google.com/ns/1.0="xmlns:g">8987983</g:id>
<title>Some cool product</title>
<g:brand http://base.google.com/ns/1.0="xmlns:g">Cool</g:brand>
<g:description http://base.google.com/ns/1.0="xmlns:g">Why is this so cool?</g:description>
<g:price http://base.google.com/ns/1.0="xmlns:g">69.00 USD</g:price>
...
What is causing this to flip?
attributes = [{'xmlns:g', 'http://base.google.com/ns/1.0'}]
This is a list containing a set, not a dictionary. Neither sets nor dictionaries are ordered.

Parsing complex Xml Python 3.4

I have the following xml :
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Suite>
<TestCase>
<TestCaseID>001</TestCaseID>
<TestCaseDescription>Hello</TestCaseDescription>
<TestSetup>
<Action>
<ActionCommand>gfdg</ActionCommand>
<TimeOut>dfgd</TimeOut>
<BamSymbol>gff</BamSymbol>
<Side>vfbgc</Side>
<PrimeBroker>fgfd</PrimeBroker>
<Size>fbcgc</Size>
<PMCode>fdgd</PMCode>
<Strategy>fdgf</Strategy>
<SubStrategy>fgf</SubStrategy>
<ActionLogEndPoint>fdgf</ActionLogEndPoint>
<IsActionResultLogged>fdgf</IsActionResultLogged>
<ValidationStep>
<IsValidated>fgdf</IsValidated>
<ValidationFormat>dfgf</ValidationFormat>
<ResponseEndpoint>gdf</ResponseEndpoint>
<ResponseParameterName>fdgfdg</ResponseParameterName>
<ResponseParameterValue>gff</ResponseParameterValue>
<ExpectedValue>fdgf</ExpectedValue>
<IsValidationResultLogged>gdfgf</IsValidationResultLogged>
<ValidationLogEndpoint>fdgf</ValidationLogEndpoint>
</ValidationStep>
</Action>
<Action>
<ActionCommand>New Order</ActionCommand>
<TimeOut>fdgf</TimeOut>
<BamSymbol>fdg</BamSymbol>
<Side>C(COVER)</Side>
<PrimeBroker>CSPB</PrimeBroker>
<Size>fdgd</Size>
<PMCode>GREE</PMCode>
<Strategy>Generalist</Strategy>
<SubStrategy>USLC</SubStrategy>
<ActionLogEndPoint>gfbhgf</ActionLogEndPoint>
<IsActionResultLogged>fdgf</IsActionResultLogged>
<ValidationStep>
<IsValidated>fdgd</IsValidated>
<ValidationFormat>dfgfd</ValidationFormat>
<ResponseEndpoint>dfgf</ResponseEndpoint>
<ResponseParameterName>fdgfd</ResponseParameterName>
<ResponseParameterValue>dfgf</ResponseParameterValue>
<ExpectedValue>fdg</ExpectedValue>
<IsValidationResultLogged>fdgdf</IsValidationResultLogged>
<ValidationLogEndpoint>fdgfd</ValidationLogEndpoint>
</ValidationStep>
</Action>
</TestCase>
</Suite>
Based on the ActionCommand i am getting either one block , the issue is could not get the sub parent tag (ValidationStep) and all its child tags . Can anyone help?
My code:
for testSetup4 in root.findall(".TestCase/TestSetup/Action"):
if testSetup4.find('ActionCommand').text == "gfdg":
for c1 in testSetup4:
t2.append(c1.tag)
v2.append(c1.text)
for k,v in zip(t2, v2):
test_case[k] = v
I am not able to get ValidationStep (sub parent) and its corresponding tags.
Simply add another loop to iterate through the <ValidationStep> node and its children. Also, you do not need the two other lists as you can update a dictionary during the parsing loop:
import xml.etree.ElementTree as et
dom = et.parse('Input.xml')
root = dom.getroot()
test_case = {}
for testSetup4 in root.findall(".TestCase/TestSetup/Action"):
if testSetup4.find('ActionCommand').text == "gfdg":
for c1 in testSetup4:
test_case[c1.tag]= c1.text
for vd in testSetup4.findall("./ValidationStep/*"):
test_case[vd.tag]= vd.text
Alternatively, use the double slash operator to search for all children including grandchildren of <Action> element:
for testSetup4 in root.findall(".TestCase/TestSetup/Action"):
if testSetup4.find('ActionCommand').text == "gfdg":
for c1 in testSetup4.findall(".//*"):
test_case[c1.tag]= c1.text

Categories