Difference between revisions of "xpath"

From Lazarus wiki
Jump to navigationJump to search
m (language)
(Add an example)
Line 2: Line 2:
 
further development recently.
 
further development recently.
  
 +
== Example ==
 +
 +
'''ts.xml'''
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0" ?><!DOCTYPE TS><TS language="es_ES" version="2.1">
 +
<context>
 +
    <name>AboutDialog</name>
 +
    <message>
 +
        <location filename="../src/lib/other/aboutdialog.ui" line="60"/>
 +
        <source>Authors</source>
 +
        <translation>Autores</translation>
 +
    </message>
 +
    <message>
 +
        <location filename="../src/lib/other/aboutdialog.cpp" line="56"/>
 +
        <source>Authors and Contributors</source>
 +
        <translation>Autores y contribuidores</translation>
 +
    </message>
 +
</context>
 +
</TS>
 +
</syntaxhighlight>
 +
<br />
 +
 +
<source>
 +
uses
 +
  DOM, XMLRead, XPath;
 +
 +
var
 +
  Xml: TXMLDocument;
 +
  XPathResult: TXPathVariable;
 +
begin
 +
  ReadXMLFile(Xml, 'ts.xml');
 +
  //Get text inside <translation> tag
 +
  XPathResult := EvaluateXPathExpression('/TS/context[name="AboutDialog"]/message[source="Authors"]/translation', Xml.DocumentElement);
 +
  ShowMessage(String(XPathResult.AsText));
 +
  XPathResult.Free;
 +
  Xml.Free;
 +
end; 
 +
</source>
 +
<br />
 +
 +
----
 
Back to [[fcl-xml]] overview.
 
Back to [[fcl-xml]] overview.
  
 
[[Category:XML]]
 
[[Category:XML]]

Revision as of 16:37, 10 March 2018

Just an XPath implementation. Should be fairly completed, but there hasn't been further development recently.

Example

ts.xml

<?xml version="1.0" ?><!DOCTYPE TS><TS language="es_ES" version="2.1">
<context>
    <name>AboutDialog</name>
    <message>
        <location filename="../src/lib/other/aboutdialog.ui" line="60"/>
        <source>Authors</source>
        <translation>Autores</translation>
    </message>
    <message>
        <location filename="../src/lib/other/aboutdialog.cpp" line="56"/>
        <source>Authors and Contributors</source>
        <translation>Autores y contribuidores</translation>
    </message>
</context>
</TS>


uses
  DOM, XMLRead, XPath;

var
  Xml: TXMLDocument;
  XPathResult: TXPathVariable;
begin
  ReadXMLFile(Xml, 'ts.xml');
  //Get text inside <translation> tag
  XPathResult := EvaluateXPathExpression('/TS/context[name="AboutDialog"]/message[source="Authors"]/translation', Xml.DocumentElement);
  ShowMessage(String(XPathResult.AsText));
  XPathResult.Free;
  Xml.Free;
end;



Back to fcl-xml overview.