XML Models

XML models are used to describe the structure of OPC UA address space: its nodes, types, variables, etc. There are many predefined XML models in the OPC UA. You can find them in the git repository NodeSet.

Loading XML models

When you start the server, you can load XML models from the file. During loading, the server will parse the XML models and build the address space. For every new loaded XML model added to the server, the server will create a new namespace.

Consider loading XML model for plastic rubber industry: PlasticRubber.

The following example shows how to load the XML models by using the HTTP protocol from OPCUA Foundation github repository:

-- Load the required modules
local ua = require("opcua.api")

local server = ua.newServer()
server:initialize()

local baseUrl = "https://raw.githubusercontent.com/OPCFoundation/UA-Nodeset/refs/heads/latest"
server:loadXmlModels({
  baseUrl.."/DI/Opc.Ua.Di.NodeSet2.xml",
  baseUrl.."/PlasticsRubber/GeneralTypes/1.03/Opc.Ua.PlasticsRubber.GeneralTypes.NodeSet2.xml"
})

server:run()
server:shutdown()

Full source

The following example shows how to load the XML models from the local file:

-- Load the required modules
local ua = require("opcua.api")

local server = ua.newServer()
server:initialize()

-- current file path
local rootPath = debug.getinfo(1, "S").short_src:match("..(/.*/)")
rootPath = ba.openio("home"):realpath(rootPath)

server:loadXmlModels({
  rootPath.."euromap83_1_03/Opc.Ua.Di.NodeSet2.xml",
  rootPath.."euromap83_1_03/Opc.Ua.PlasticsRubber.GeneralTypes.NodeSet2.xml"
})

server:run()
server:shutdown()

Full source

Export XML models

You can export XML models from the server to the file. It is possible to export either all models or selected models by specifying namespace URIs. The following example shows how to export XML models to the file:

-- Load XML models from the OPCUA Foundation github repository
local baseUrl = "https://raw.githubusercontent.com/OPCFoundation/UA-Nodeset/refs/heads/latest"
server:loadXmlModels({
  baseUrl.."/DI/Opc.Ua.Di.NodeSet2.xml",
  baseUrl.."/PlasticsRubber/GeneralTypes/1.03/Opc.Ua.PlasticsRubber.GeneralTypes.NodeSet2.xml"
})

-- Method 1: Export all models to a file
local outputFile = "exported_models.xml"
server:exportXmlModels(outputFile)

print("Exported all models to: " .. outputFile)

Full source