XML - Information

Use XML file as database with TF6420 Database Server

Execute XPath querries at a XML file wiht the TF6420 Database Server

You can find further information about XML-Schema here: http://www.w3.org/TR/xmlschema-0/

XML as Database

XSD-Schema for standard table structure

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:simpleType name="bigint">
    <xsd:restriction base="xsd:long" />
  </xsd:simpleType>
  <xsd:simpleType name="datetime">
    <xsd:restriction base="xsd:dateTime" />
  </xsd:simpleType>
  <xsd:simpleType name="ntext_80">
    <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80" />
    </xsd:restriction>
  </xsd:simpleType>
  <xsd:simpleType name="float">
    <xsd:restriction base="xsd:double" />
  </xsd:simpleType>
  <xsd:complexType name="myTable_Double_Type">
    <xsd:sequence>
      <xsd:element minOccurs="0" maxOccurs="unbounded" name="row">
        <xsd:complexType>
          <xsd:attribute name="ID" type="bigint" />
          <xsd:attribute name="Timestamp" type="datetime" />
          <xsd:attribute name="Name" type=" ntext_80" />
          <xsd:attribute name="Value" type="float" />
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="TestDB_XML">
    <xsd:complexType>
      <xsd:sequence minOccurs="1" maxOccurs="1">
        <xsd:element name="myTable_Double" type="myTable_Double_Type" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

XML file for standard table structure (Sample)

<?xml version="1.0" encoding="UTF-8"?>
<TestDB_XML xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="TestDB_XML.xsd">
  <myTable_Double>
    <row ID="1" Timestamp="2012-03-08T12:45:08" Name="TestValue1" Value="222.222" />
    <row ID="2" Timestamp="2012-03-08T12:45:14" Name="TestValue1" Value="222.222" />
    <row ID="3" Timestamp="2012-03-08T12:45:18" Name="TestValue1" Value="222.222" />
    <row ID="4" Timestamp="2012-03-08T12:45:22" Name="TestValue1" Value="222.222" />
    <row ID="5" Timestamp="2012-03-08T12:45:23" Name="TestValue1" Value="222.222" />
 </myTable_Double>
</TestDB_XML>

Datatypes for XML tables

  <xsd:simpleType name="bigint">
    <xsd:restriction base="xsd:long" />
  </xsd:simpleType>
 
  <xsd:simpleType name="datetime">
    <xsd:restriction base="xsd:dateTime" />
  </xsd:simpleType>
 
 <xsd:simpleType name="ntext_80"> <!-- Length can be set individually. -->
   <xsd:restriction base="xsd:string">
      <xsd:maxLength value="80" />
    </xsd:restriction>
  </xsd:simpleType>
 
  <xsd:simpleType name="float">
    <xsd:restriction base="xsd:double" />
  </xsd:simpleType>
 
  <xsd:simpleType name="binary_1"> <!-- Length can be set individually. -->
   <xsd:restriction base="xsd:hexBinary">
      <xsd:maxLength value="1" />
    </xsd:restriction>
  </xsd:simpleType>
 
  <xsd:simpleType name="bit">
    <xsd:restriction base="xsd:boolean" />
  </xsd:simpleType>
 
 <xsd:simpleType name="image_1"> <!-- Length can be set individually. -->
   <xsd:restriction base="xsd:hexBinary">
      <xsd:maxLength value="1" />
    </xsd:restriction>
  </xsd:simpleType>
 
  <xsd:simpleType name="integer">
    <xsd:restriction base="xsd:int" />
  </xsd:simpleType>
 
  <xsd:simpleType name="money">
    <xsd:restriction base="xsd:double" />
  </xsd:simpleType>
 
  <xsd:simpleType name="nchar_50"> <!-- Length can be set individually.-->
   <xsd:restriction base="xsd:string">
      <xsd:maxLength value="50" />
    </xsd:restriction>
  </xsd:simpleType>
 
  <xsd:simpleType name="nvarchar_50"> <!-- Length can be set individually.-->
   <xsd:restriction base="xsd:string">
      <xsd:maxLength value="50" />
    </xsd:restriction>
  </xsd:simpleType>
 
  <xsd:simpleType name="real">
    <xsd:restriction base="xsd:double" />
  </xsd:simpleType>
 
  <xsd:simpleType name="smallint">
    <xsd:restriction base="xsd:short" />
  </xsd:simpleType>
 
  <xsd:simpleType name="tinyint">
    <xsd:restriction base="xsd:byte" />
  </xsd:simpleType>
 
  <xsd:simpleType name="varbinary_1"> <!-- Length can be set individually.-->
   <xsd:restriction base="xsd:hexBinary">
      <xsd:maxLength value="1" />
    </xsd:restriction>
 </xsd:simpleType>

Datatype mapping XML => PLC

E_DBColumnTypes

XML

PLC Control

eDBColumn_BigInt

xsd:long

T_LARGE_INTEGER (TcUtilities.lib)

eDBColumn_Integer

xsd:int

DINT

eDBColumn_SmallInt

xsd:short

INT

eDBColumn_TinyInt

xsd:byte

BYTE

eDBColumn_Bit

xsd:boolean

BOOL

eDBColumn_Money

xsd:double

LREAL

eDBColumn_Float

xsd:double

LREAL

eDBColumn_Real

xsd:double

LREAL

eDBColumn_DateTime

xsd:dateTime

DT

eDBColumn_NText

xsd:string

STRING

eDBColumn_NChar

xsd:string

STRING

eDBColumn_Image

xsd:hexBinary

ARRAY OF BYTE

eDBColumn_NVarChar

xsd:string

STRING

eDBColumn_Binary

xsd:hexBinary

ARRAY OF BYTE

eDBColumn_VarBinary

xsd:hexBinary

ARRAY OF BYTE

Creating/Reading of data records into/from an XML file

For creating data records, it is possible to use standard SQL commands. The SQL INSERT commands will be interpreted of the TwinCAT3 Database Server and creates the specified XML-Nodes for the used XML file. The SQL SELECT commands will be converted to XPath queries which will be executed at the used XML file.

Samples for supported INSERT commands:

INSERT INTO myTable_Double (ID, Timestamp, Name, Value) VALUES(1, CURRENT_TIMESTAMP, 'TestValue1' , 1234.5678)
INSERT INTO myTable_Double (Timestamp, Name) VALUES(CURRENT_TIMESTAMP, 'TestValue1');
INSERT INTO myTable_Double VALUES(1, CURRENT_TIMESTAMP, 'TestValue1', 1234.5678);
INSERT INTO myTable_Double VALUES(1, '2010-01-06 12:13:14', 'TestValue1', 1234.5678);

Samples for supported SELECT commands:

SELECT ID, Timestamp, Name, Value FROM myTable_Double;
SELECT * FROM myTable_Double;
SELECT Timestamp, Name FROM myTable_Double
SELECT * FROM myTable_Double WHERE Name = 'TestValue1';
SELECT * FROM myTable_Double WHERE ID > 1;

Supported function blocks

FB_DBCreate

FB_DBCyclicRdWrt

FB_DBRead

FB_DBRecordArraySelect

FB_DBRecordDelete

FB_DBRecordInsert

FB_DBRecordInsert_EX

FB_DBRecordSelect

FB_DBRecordSelect_EX

FB_DBTableCreate

FB_DBWrite

XML standard XPath function

XPath Types

3 different modes are supported to read values of an XML file...

Samples

XML file:

<?xml version="1.0" encoding="utf-8" ?>
<TestXML>
  <Node attr1="1" attr2="Node1">
    <SubNode1>SubNodeWert1</SubNode1>
    <SubNode2>200</SubNode2>
    <SubNode3>SubNodeWert3</SubNode3>
    <SubNode4>400.5</SubNode4>
    <SubNode5>SubNodeWert5</SubNode5>
  </Node>
  <Node attr1="2" attr2="Node2">
    <SubNode1>SubNodeWert1</SubNode1>
    <SubNode2>200</SubNode2>
    <SubNode3>SubNodeWert3</SubNode3>
    <SubNode4>400.5</SubNode4>
    <SubNode5>SubNodeWert5</SubNode5>
  </Node>
</TestXML>

XML schema:

<? xml version="1.0" encoding="utf-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="TestXML">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="Node">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="SubNode1" type="xs:string" />
              <xs:element name="SubNode2" type="xs:short" />
              <xs:element name="SubNode3" type="xs:string" />
              <xs:element name="SubNode4" type="xs:double" />
              <xs:element name="SubNode5" type="xs:string" />
            </xs:sequence>
            <xs:attribute name ="attr1" type="xs:integer" use="required" />
            <xs:attribute name ="attr2" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sample for XPATH<ATTR>

XPath => XPATH<ATTR>#TestXML/Node[@attr1=2]

Returned structure if no XML schema is available.

TYPE ST_Record :
STRUCT
    attr1 : attr1 : T_MaxString := ‘2’;
    attr2 : T_MaxString := ‘Node2’;’;
END_STRUCT
END_TYPE

Returned structure if a XML schema is available.

TYPE ST_Record :
STRUCT
    attr1 : DINT := 2;
    attr2 : T_MaxString := ‘Node2’;
END_STRUCT
END_TYPE

Sample for XPATH<TAG>

XPath => XPATH<TAG>#TestXML/Node[@attr1=2]/SubNode2

Returned value if no XML schema is available: SubNode2 : T_MaxString := ‘200’;

Returned value if an XML schema is available: SubNode2 : INT := 200;

Sample for XPATH<SUBTAG>

XPath => XPATH<SUBTAG>#TestXML/Node[@attr1=2]

Returned structure if no XML schema is available.

TYPE ST_Record :
STRUCT
    SubNode1 : T_MaxString := ‘SubNodeWert1’;
    SubNode2 : T_MaxString := ‘200’;
    SubNode3 : T_MaxString := ‘SubNodeWert3’;
    SubNode4 : T_MaxString := ‘400.5’;
    SubNode5 : T_MaxString := ‘SubNodeWert5’;
END_STRUCT
END_TYPE

Returned structure if a XML schema is available.

TYPEYPE ST_Record :
STRUCT
    SubNode1 : T_MaxString := ‘SubNodeWert1’;
    SubNode2 : INT := 200;
    SubNode3 : T_MaxString := ‘SubNodeWert3’;
    SubNode4 : LREAL := 400.5;
    SubNode5 : T_MaxString := ‘SubNodeWert5’;
END_STRUCT
END_TYPE

Supported function blocks

FB_DBRecordSelect

FB_DBRecordSelect_EX

FB_DBRecordArraySelect