Complete preliminary XSY reading code.

This commit is contained in:
2026-01-29 10:45:11 -06:00
parent d809d43823
commit d141543691
3 changed files with 72 additions and 45 deletions

View File

@@ -4,28 +4,46 @@
Copyright 2026 Prime Controls. All rights reserved. Copyright 2026 Prime Controls. All rights reserved.
--> -->
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/> <xsl:output encoding="utf8" method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/> <!-- <xsl:strip-space elements="*"/> -->
<xsl:template match="/VariablesExchangeFile"> <xsl:template match="/VariablesExchangeFile">
<xsl:copy> <xsl:copy>
<xsl:apply-templates select="dataBlock"/>
<xsl:apply-templates select="DDTSource"/> <xsl:apply-templates select="DDTSource"/>
</xsl:copy> </xsl:copy>
</xsl:template> </xsl:template>
<xsl:template match="dataBlock">
<xsl:apply-templates select="variables"/>
</xsl:template>
<xsl:template match="DDTSource"> <xsl:template match="DDTSource">
<xsl:apply-templates select="structure/variables"/>
</xsl:template>
<xsl:template match="variables">
<xsl:copy> <xsl:copy>
<xsl:apply-templates select="structure/variables"/> <name><xsl:value-of select="@name"/></name>
<parent/>
<type><xsl:value-of select="@typeName"/></type>
<address><xsl:value-of select="@topologicalAddress"/></address>
<bitrank/>
<save><xsl:value-of select="attribute/@Save"/></save>
<comment><xsl:value-of select="../../comment"/>: <xsl:value-of select="comment"/></comment>
</xsl:copy> </xsl:copy>
</xsl:template> </xsl:template>
<xsl:template match="structure/variables"> <xsl:template match="structure/variables">
<!-- <xsl:copy> --> <xsl:copy>
<name><xsl:value-of select="../../@DDTName"/>.<xsl:value-of select="@name"/></name> <name><xsl:value-of select="@name"/></name>
<parent><xsl:value-of select="../../@DDTName"/></parent>
<type><xsl:value-of select="@typeName"/></type> <type><xsl:value-of select="@typeName"/></type>
<address/>
<bitrank><xsl:value-of select="attribute/@ExtractBit"/></bitrank> <bitrank><xsl:value-of select="attribute/@ExtractBit"/></bitrank>
<save/>
<comment><xsl:value-of select="../../comment"/>: <xsl:value-of select="comment"/></comment> <comment><xsl:value-of select="../../comment"/>: <xsl:value-of select="comment"/></comment>
<!-- </xsl:copy> --> </xsl:copy>
</xsl:template> </xsl:template>
</xsl:stylesheet> </xsl:stylesheet>

View File

@@ -1,47 +1,38 @@
import xmltodict
import pandas as pd import pandas as pd
import sys
from iec import datatypes, ElementaryVariable, UDT from iec import datatypes, ElementaryVariable, UDT
from io import StringIO
from lxml import etree
from pandas import DataFrame
def get_ddts(): path = 'D:/OneDrive - Prime Controls/Repositories/AuDaGe/formats/controlexpert/'
"""Extract the DDT definitions from an XSY file."""
ddt_sources = root.findall('DDTSource') def read_xsy(xsypath, sspath, outpath) -> DataFrame:
for ddt in ddt_sources:
df = pd.read_xml(ddt)
def handle_element(path, item):
nodename = path[1][0]
match nodename:
case 'DDTSource':
ddtname = item['@DDTName']
ddtcomment = item['comment']
ddtvariables = dict()
for var in item['structure']['variables']:
ddtvariables[var['@name']] = ElementaryVariable(
name = var['@name'],
type = var['@typeName'],
address = 0,
comment = var['comment']
)
print(f"DDT Name: {ddtname}")
print(f"DDT Comment: {ddtcomment}")
print(f"DDT Variables: {ddtvariables}")
case 'dataBlock':
pass
return True # Continue processing
def read_xsy(inputfile):
"""Read an XSY file into a dataframe.""" """Read an XSY file into a dataframe."""
with open(inputfile, 'rb') as f: with open(sspath) as ssfile:
xmltodict.parse( ssxml = etree.XML(ssfile.read())
f, sstransform = etree.XSLT(ssxml)
item_depth=2, with open(xsypath) as xsyfile:
item_callback=handle_element xsyxml = etree.parse(xsyfile)
) xsy = sstransform(xsyxml)
xsy.write_output(outpath + '.xml')
# data_block = root.find('dataBlock') df = pd.read_xml(StringIO(str(xsy)))
# print(df)
df.to_csv(outpath + '.csv', index=False)
if __name__ == '__main__': if __name__ == '__main__':
read_xsy('D:\\OneDrive - Prime Controls\\Repositories\\AuDaGe\\formats\\controlexpert\\builtins.xsy') try:
xsypath = sys.argv[1]
except:
xsypath = 'builtins.xsy'
try:
sspath = sys.argv[2]
except:
sspath = 'flatten_xsy.xslt'
try:
outpath = sys.argv[3]
except:
outpath = 'test/test'
read_xsy(path + xsypath, path + sspath, path + outpath)

View File

@@ -0,0 +1,18 @@
# XSY Structure
- VariablesExchangeFile
- ~~fileHeader~~
- dataBlock
- variables+ (name, typeName, [topologicalAddress])
- attribute+ (name=(Save), value=(-1))
- comment
- value
- instanceElementDesc+ (name)
- value
- ~~FBSource+~~
- DDTSource+
- attribute+
- structure
- variables+
- attribute+ (name=(ExtractBit), value)
- comment