XML XPath Sample for Visualisation

With the help of the function block FB_DBRecordSelect it is possible to send XPath querries to select XML-Tags of various XML files. This sample shows, how to select entries of dynamic textlists (XML-file) for the TwinCAT PLC Visualisation with the TF6420 Database Server.

Download "Beispiel mit XPath und TargetVisu" sample10.zip

XML XPath Sample for Visualisation 1:

Used database type

XML

Compatible database type

XML

Used function blocks

FB_DBRecordSelect

Integrated libraries

"TcDatabase.lib", "TcSystem.lib","TcBase.lib","TcStandard.lib","TcUtilities.lib"

Download Data list

XPath_GetText.pro, CurrentConfigDatabase.xml, VisuTest.xml

Dynamic text list for TwinCAT PLC Visualization

<?xml version="1.0" encoding="ISO-8859-1"?>
<dynamic-text>
  <header>
   <default-language>deutsch</default-language>
    <default-font>
      <language>deutsch</language>
      <font-name>Arial </font-name>
      <font-color>0,0,0</font-color>
      <font-height>-13</font-height>
      <font-weight>700</font-weight>
      <font-italic>false</font-italic>
      <font-underline>false</font-underline>
     <font-strike-out>false</font-strike-out>
      <font-char-set>0</font-char-set>
    </default-font>
    <default-font>
      <language>english</language>
      <font-name>Arial </font-name>
      <font-color>0,0,0</font-color>
     <font-height>-13</font-height>
      <font-weight>700</font-weight>
      <font-italic>false</font-italic>
      <font-underline>false</font-underline>
     <font-strike-out>false</font-strike-out>
      <font-char-set>0</font-char-set>
    </default-font>
     <default-font>
      <language>francais</language>
      <font-name>Arial </font-name>
      <font-color>0,0,0</font-color>
     <font-height>-13</font-height>
      <font-weight>700</font-weight>
      <font-italic>false</font-italic>
      <font-underline>false</font-underline>
     <font-strike-out>false</font-strike-out>
      <font-char-set>0</font-char-set>
    </default-font>
  </header>
  <text-list>
   <text prefix="A" id="1">
      <deutsch>Datei öffnen...</deutsch>
     <english>File open...</english>
      <francais>Fichier ouvrir...</francais>
    </text>
    <text prefix="B" id="2">
     <deutsch>Datei schließen</deutsch>
      <english>File close...</english>
     <francais>Fermer le fichier...</francais>
    </text>
    <text prefix="C" id="3">
     <deutsch>Deutschland</deutsch>
      <english>England</english>
     <francais>France</francais>
    </text>
  </text-list>
</dynamic-text>

Function block "FB_GetText" (to read the XML-Tags)

FUNCTION_BLOCKFB_GetText
VAR_INPUT
    dwID: DWORD;
    stPrefix: T_MaxString;
    stLanguage : T_MaxString;
    bExecute : BOOL;
END_VAR

VAR_OUTPUT
    bBusy : BOOL;
    bError : BOOL;
    nResultLength : INT;
    stResult : STRING(256);
END_VAR

VAR
    R_TRIG1: R_TRIG;
    state: BYTE;
    FB_DBRecordSelect1: FB_DBRecordSelect;
    FB_FormatString1: FB_FormatString;
END_VAR
R_TRIG1(CLK:=bExecute);
IF R_TRIG1.Q THEN
    state := 1;
    bBusy := TRUE;
    bError := FALSE;
    FB_DBRecordSelect1(bExecute:=FALSE);
END_IF
CASE
 state OF
    0:
    ;
    1:
    FB_FormatString1(
        sFormat:= 'XPATH<TAG>#/dynamic-text/text-list/text[@prefix=$'%s$' and @id=%d]/%s',
        arg1:= F_STRING(stPrefix),
        arg2:= F_DWORD(dwID),
        arg3:= F_STRING(stLanguage),
        sOut=> FB_DBRecordSelect1.sSelectCmd);

    FB_DBRecordSelect1(
        sNetID:= ,
        hDBID:= 1,
        nRecordIndex:= 0,
        cbRecordSize:= SIZEOF(stResult),
        pDestAddr:= ADR(stResult),
        bExecute:= TRUE,
        tTimeout:= T#10s);

    IF NOT FB_DBRecordSelect1.bBusy THEN
        IF NOT FB_DBRecordSelect1.bError THEN
            nResultLength := LEN(stResult);
        ELSE
            bError := TRUE;
        END_IF
        bBusy := FALSE;
        state := 0;
    END_IF
END_CASE

MAIN Program

PROGRAMMAIN 
VAR
    FB_GetText1: FB_GetText;
    startstop: BOOL;
    busy: BOOL;
    err: BOOL;
    resultLen: INT;
    result: STRING(256);
END_VAR
FB_GetText1(
    dwID:= 1,
    stPrefix:= 'A',
    stLanguage:= 'deutsch',
    bExecute:= startstop,
    bBusy=> busy,
    bError=> err,
    nResultLength=> resultLen,
    stResult=> result); 

The function block FB_GetText will be execute with a rising edge at the input variable bExecute. The received text contains the output variable stResult. The length of the text will be returned at the output variable nResultLength.