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.
-->
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:output encoding="utf8" method="xml" omit-xml-declaration="no" indent="yes"/>
<!-- <xsl:strip-space elements="*"/> -->
<xsl:template match="/VariablesExchangeFile">
<xsl:copy>
<xsl:apply-templates select="dataBlock"/>
<xsl:apply-templates select="DDTSource"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dataBlock">
<xsl:apply-templates select="variables"/>
</xsl:template>
<xsl:template match="DDTSource">
<xsl:apply-templates select="structure/variables"/>
</xsl:template>
<xsl:template match="variables">
<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:template>
<xsl:template match="structure/variables">
<!-- <xsl:copy> -->
<name><xsl:value-of select="../../@DDTName"/>.<xsl:value-of select="@name"/></name>
<xsl:copy>
<name><xsl:value-of select="@name"/></name>
<parent><xsl:value-of select="../../@DDTName"/></parent>
<type><xsl:value-of select="@typeName"/></type>
<address/>
<bitrank><xsl:value-of select="attribute/@ExtractBit"/></bitrank>
<save/>
<comment><xsl:value-of select="../../comment"/>: <xsl:value-of select="comment"/></comment>
<!-- </xsl:copy> -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

View File

@@ -1,47 +1,38 @@
import xmltodict
import pandas as pd
import sys
from iec import datatypes, ElementaryVariable, UDT
from io import StringIO
from lxml import etree
from pandas import DataFrame
def get_ddts():
"""Extract the DDT definitions from an XSY file."""
path = 'D:/OneDrive - Prime Controls/Repositories/AuDaGe/formats/controlexpert/'
ddt_sources = root.findall('DDTSource')
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):
def read_xsy(xsypath, sspath, outpath) -> DataFrame:
"""Read an XSY file into a dataframe."""
with open(inputfile, 'rb') as f:
xmltodict.parse(
f,
item_depth=2,
item_callback=handle_element
)
# data_block = root.find('dataBlock')
with open(sspath) as ssfile:
ssxml = etree.XML(ssfile.read())
sstransform = etree.XSLT(ssxml)
with open(xsypath) as xsyfile:
xsyxml = etree.parse(xsyfile)
xsy = sstransform(xsyxml)
xsy.write_output(outpath + '.xml')
df = pd.read_xml(StringIO(str(xsy)))
# print(df)
df.to_csv(outpath + '.csv', index=False)
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