basyx-typescript-sdk 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +1,29 @@
1
1
  import { jsonization } from '@aas-core-works/aas-core3.1-typescript';
2
2
  import { AdministrativeInformation, AnnotatedRelationshipElement, AssetAdministrationShell, AssetInformation, AssetKind, BasicEventElement, Blob, Capability, ConceptDescription, DataSpecificationIec61360, DataTypeDefXsd, EmbeddedDataSpecification, Entity, Extension, File, Key, KeyTypes, LangStringDefinitionTypeIec61360, LangStringNameType, LangStringPreferredNameTypeIec61360, LangStringShortNameTypeIec61360, LangStringTextType, LevelType, MultiLanguageProperty, Operation, OperationVariable, Property, Qualifier, Range, Reference, ReferenceElement, ReferenceTypes, RelationshipElement, Resource, SpecificAssetId, Submodel, SubmodelElementCollection, SubmodelElementList, ValueList, ValueReferencePair, } from '@aas-core-works/aas-core3.1-typescript/types';
3
- import { XMLBuilder, XMLParser } from 'fast-xml-parser';
3
+ import { buildObject, buildXmlNode, parseXmlString, serializeXml as serializeXmlNode } from 'xml-sax-ts';
4
4
  import { BaSyxEnvironment } from '../../models/BaSyxEnvironment';
5
5
  export function serializeXml(data, namespacePrefix = 'aas') {
6
6
  // Return empty string for empty environment
7
7
  if (!data.assetAdministrationShells?.length && !data.submodels?.length && !data.conceptDescriptions?.length) {
8
8
  return '';
9
9
  }
10
- const builder = new XMLBuilder({
11
- ignoreAttributes: false,
12
- attributeNamePrefix: '@_',
13
- attributeValueProcessor: (name, val) => val,
14
- format: true,
15
- indentBy: ' ',
16
- suppressEmptyNode: true,
17
- suppressBooleanAttributes: false,
18
- processEntities: false,
19
- cdataPropName: '__cdata',
20
- arrayNodeName: 'element',
21
- suppressUnpairedNode: false,
22
- unpairedTags: [],
23
- tagValueProcessor: (name, val) => val,
24
- });
25
10
  // Transform and add namespace prefix to all elements
26
11
  const envData = transformEnvironmentToXmlStructure(data);
27
12
  const prefixedData = addNamespacePrefix(envData, namespacePrefix);
28
- // Wrap the environment data with proper XML declaration and namespace
29
- const xmlObject = {
30
- '?xml': {
31
- '@_version': '1.0',
32
- '@_encoding': 'UTF-8',
33
- },
13
+ const rootNode = buildXmlNode({
34
14
  [`${namespacePrefix}:environment`]: {
35
15
  [`@_xmlns:${namespacePrefix}`]: 'https://admin-shell.io/aas/3/1',
36
16
  '@_xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
37
17
  '@_xsi:schemaLocation': 'https://admin-shell.io/aas/3/1 AAS.xsd',
38
18
  ...prefixedData,
39
19
  },
40
- };
41
- let xml = builder.build(xmlObject);
20
+ });
21
+ let xml = serializeXmlNode(rootNode, {
22
+ pretty: true,
23
+ xmlDeclaration: true,
24
+ indent: ' ',
25
+ newline: '\n',
26
+ });
42
27
  // Format the root element attributes to be on separate lines
43
28
  const escapedPrefix = namespacePrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
44
29
  const regex = new RegExp(`<${escapedPrefix}:environment xmlns:${escapedPrefix}="https:\\/\\/admin-shell\\.io\\/aas\\/3\\/1" xmlns:xsi="http:\\/\\/www\\.w3\\.org\\/2001\\/XMLSchema-instance" xsi:schemaLocation="https:\\/\\/admin-shell\\.io\\/aas\\/3\\/1 AAS\\.xsd">`, 'g');
@@ -897,40 +882,14 @@ export function deserializeXml(xmlString) {
897
882
  if (!xmlString || xmlString.trim() === '') {
898
883
  return new BaSyxEnvironment();
899
884
  }
900
- const parser = new XMLParser({
901
- ignoreAttributes: false,
902
- attributeNamePrefix: '@_',
903
- parseAttributeValue: false,
904
- parseTagValue: false,
905
- trimValues: true,
906
- removeNSPrefix: true, // Remove namespace prefixes like aas:
907
- isArray: (name) => {
908
- // Define which elements should always be treated as arrays
909
- const arrayElements = [
910
- 'extension',
911
- 'langStringNameType',
912
- 'langStringTextType',
913
- 'langStringPreferredNameTypeIec61360',
914
- 'langStringShortNameTypeIec61360',
915
- 'langStringDefinitionTypeIec61360',
916
- 'reference',
917
- 'key',
918
- 'valueReferencePair',
919
- 'embeddedDataSpecification',
920
- 'specificAssetId',
921
- 'assetAdministrationShell',
922
- 'submodel',
923
- 'conceptDescription',
924
- 'qualifier',
925
- 'operationVariable',
926
- ];
927
- return arrayElements.includes(name);
928
- },
885
+ const rootNode = parseXmlString(xmlString);
886
+ const parsed = buildObject(rootNode, {
887
+ stripNamespaces: true,
888
+ arrayElements: ARRAY_ELEMENTS,
929
889
  });
930
- const parsed = parser.parse(xmlString);
931
890
  const env = new BaSyxEnvironment();
932
891
  // Extract the environment data
933
- const envData = parsed.environment;
892
+ const envData = parsed.environment ?? parsed;
934
893
  if (!envData) {
935
894
  return env;
936
895
  }
@@ -953,6 +912,24 @@ export function deserializeXml(xmlString) {
953
912
  }
954
913
  return env;
955
914
  }
915
+ const ARRAY_ELEMENTS = new Set([
916
+ 'extension',
917
+ 'langStringNameType',
918
+ 'langStringTextType',
919
+ 'langStringPreferredNameTypeIec61360',
920
+ 'langStringShortNameTypeIec61360',
921
+ 'langStringDefinitionTypeIec61360',
922
+ 'reference',
923
+ 'key',
924
+ 'valueReferencePair',
925
+ 'embeddedDataSpecification',
926
+ 'specificAssetId',
927
+ 'assetAdministrationShell',
928
+ 'submodel',
929
+ 'conceptDescription',
930
+ 'qualifier',
931
+ 'operationVariable',
932
+ ]);
956
933
  function parseAssetAdministrationShell(data) {
957
934
  return new AssetAdministrationShell(data.id, parseAssetInformation(data.assetInformation), data.extensions?.extension ? data.extensions.extension.map(parseExtension) : undefined, data.category || undefined, data.idShort || undefined, data.displayName?.langStringNameType
958
935
  ? data.displayName.langStringNameType.map(parseLangStringNameType)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "basyx-typescript-sdk",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "BaSyx TypeScript SDK for developing applications and components for the Asset Administration Shell (AAS)",
5
5
  "main": "bundle/index.cjs",
6
6
  "module": "bundle/index.mjs",
@@ -64,6 +64,6 @@
64
64
  "typescript-eslint": "^8.21.0"
65
65
  },
66
66
  "dependencies": {
67
- "fast-xml-parser": "^5.3.3"
67
+ "xml-sax-ts": "^0.3.0"
68
68
  }
69
69
  }