Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Sunday, January 5, 2025

Parse text colum from mtConcepts of a SDLTB file with Python XML or BeautifulSoup

 from bs4 import BeautifulSoup

# XML data

xml_data = """<cG><c>1</c><trG><tr type="origination">letco</tr><dt>2020-08-30T19:12:50</dt></trG><trG><tr type="modification">letco</tr><dt>2020-08-30T19:12:58</dt></trG><lG><l lang="DE" type="German"/><tG><t>Pelletpresse</t><trG><tr type="origination">letco</tr><dt>2020-08-30T19:12:50</dt></trG><trG><tr type="modification">letco</tr><dt>2020-08-30T19:12:50</dt></trG></tG></lG><lG><l lang="RO" type="Romanian"/><tG><t>presă de peleți</t><trG><tr type="origination">letco</tr><dt>2020-08-30T19:12:58</dt></trG><trG><tr type="modification">letco</tr><dt>2020-08-30T19:12:58</dt></trG></tG></lG></cG>"""


# Parse the XML

soup = BeautifulSoup(xml_data, "xml")


# Function to extract translation details

def parse_translation(translation_element):

lang = translation_element.find("l").attrs

text = translation_element.find("t").text

transactions = [

{

"type": tr.find("tr")["type"],

"actor": tr.find("tr").text,

"datetime": tr.find("dt").text,

}

for tr in translation_element.find_all("trG")

]

return {

"lang": lang["lang"],

"type": lang["type"],

"text": text,

"transactions": transactions,

}


# Extract the main data

data = {

"c": soup.find("c").text,

"transactions": [

{

"type": tr.find("tr")["type"],

"actor": tr.find("tr").text,

"datetime": tr.find("dt").text,

}

for tr in soup.find_all("trG", recursive=False)

],

"translations": [parse_translation(lG) for lG in soup.find_all("lG")],

}


# Output the extracted data

print(data)



from xml.etree import ElementTree as ET


# Parse the XML

root = ET.fromstring(xml_data)


# Function to extract translation details

def parse_translation(translation_element):

lang = translation_element.find("./l").attrib

text = translation_element.find("./tG/t").text

transactions = [

{

"type": tr.find("./tr").attrib["type"],

"actor": tr.find("./tr").text,

"datetime": tr.find("./dt").text,

}

for tr in translation_element.findall("./tG/trG")

]

return {

"lang": lang["lang"],

"type": lang["type"],

"text": text,

"transactions": transactions,

}


# Extract the main data

data = {

"c": root.find("./c").text,

"transactions": [

{

"type": tr.find("./tr").attrib["type"],

"actor": tr.find("./tr").text,

"datetime": tr.find("./dt").text,

}

for tr in root.findall("./trG")

],

"translations": [parse_translation(lG) for lG in root.findall("./lG")],

}


# Output the extracted data

print(data)



Monday, December 7, 2020

Removing All XML or HTML Tags with Notepad++ RegEx

Open Notepad++
Go to Find and Replace.
Enter this regular expression:
<[^>]+>
Select regular expression. Make sure the cursor is at the start of the document.
Click replace all

Saturday, January 13, 2018

Microsoft Serialization.xsd Schema for API response

<?xml version="1.0" encoding="utf-8"?>
 <xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="anyType" nillable="true" type="xs:anyType" />
   <xs:element name="anyURI" nillable="true" type="xs:anyURI" />
   <xs:element name="base64Binary" nillable="true" type="xs:base64Binary" />
   <xs:element name="boolean" nillable="true" type="xs:boolean" />
   <xs:element name="byte" nillable="true" type="xs:byte" />
   <xs:element name="dateTime" nillable="true" type="xs:dateTime" />
   <xs:element name="decimal" nillable="true" type="xs:decimal" />
   <xs:element name="double" nillable="true" type="xs:double" />
   <xs:element name="float" nillable="true" type="xs:float" />
   <xs:element name="int" nillable="true" type="xs:int" />
   <xs:element name="long" nillable="true" type="xs:long" />
   <xs:element name="QName" nillable="true" type="xs:QName" />
   <xs:element name="short" nillable="true" type="xs:short" />
   <xs:element name="string" nillable="true" type="xs:string" />
   <xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte" />
   <xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt" />
   <xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong" />
   <xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort" />
   <xs:element name="char" nillable="true" type="tns:char" />
   <xs:simpleType name="char">
     <xs:restriction base="xs:int" />
   </xs:simpleType>
   <xs:element name="duration" nillable="true" type="tns:duration" />
   <xs:simpleType name="duration">
     <xs:restriction base="xs:duration">
       <xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?" />
       <xs:minInclusive value="-P10675199DT2H48M5.4775808S" />
       <xs:maxInclusive value="P10675199DT2H48M5.4775807S" />
     </xs:restriction>
   </xs:simpleType>
   <xs:element name="guid" nillable="true" type="tns:guid" />
   <xs:simpleType name="guid">
     <xs:restriction base="xs:string">
       <xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
     </xs:restriction>
   </xs:simpleType>
   <xs:attribute name="FactoryType" type="xs:QName" />
   <xs:attribute name="Id" type="xs:ID" />
   <xs:attribute name="Ref" type="xs:IDREF" />
 </xs:schema>