XML/XPath transfer object
XML transfer objects are available as sources and destinations. XPath queries allow elements to be extracted from XML files or written to XML files. A plug-in configuration is not necessary, you can directly configure the transfer object within the connection as described.
After double-clicking on the transfer object, set the following properties:
Section: X-Path Elements
Name | Name where the query appears in the transfer object. |
X-Path Expression | Valid X-Path query, in the simplest case the path to the node (see below). |
XML Mode | Indicates if only the content of the query result or the enclosing tag will be output (e.g. Heinrich Mann or <author>Heinrich Mann</author> . When writing to the object (Create XML), only content must always be specified because the structure is already established. By writing at the object (create XML), only content must always be specified because the structure is already established. |
X-Path Placeholder
Name of parameter | Name of the parameter |
Default value | Default value of the placeholder |
Section: Namespaces and Prefixes
Prefixes | Unique prefix |
Namespace | Namespace |
When importing the XML code at design time, all existing namespaces are entered. Use the checkbox to mark the namespace that should be used as the default for the XPath queries. If a standard is defined, formulate the X-Path queries without a prefix, this will be added automatically. In the source code, you have to specify the prefixes always,.
Section: XML Code
The multiline field is for entering XML code. At design time you can use it to check your X-Path expressions. Then you can delete the code if you dynamically pass the XML code at runtime (transfer object as source) or first generate (transfer object as destination). Of course, you can also leave static code here if that meets your requirement. |
XML documents as source
As a transfer source, the XML object extracts content from XML code based on the configured X-Path queries.
- The XML code is either set statically at design time or submit at run time to the element “XML”.
- The results are read from the X-Path query elements.
For example, the following code is supplied from a file:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>J.R.R. Tolkien</author>
<title>The Hobbit</title>
</book>
<book id="bk102">
<author>Joanne K. Rowling</author>
<title>Harry Potter and the Philosopher's Stone</title>
</book>
</catalog>
Then two X-Path expressions would supply the author and title of the books:
- Name: “Author”, X-Path expression “/catalog/book/author”
- Name: “Title”, X-Path expression “/catalog/book/title”
On the Internet you will find different documentations about the possibilities of X-Path, these are just the simplest (e.g. by http://www.w3schools.com/xsl/xpath_syntax.asp ; Restriction: Any nodes must be initiated with “//”).
For this example, the configuration looks like this:
- The path and name of the file are transferred from a constant object to the file transfer object. The name could also be determined dynamically from a database.
- The file transfer object transfers the contents of the XML file to the XML element in the XML object. At design time no code was deposited here, only the two described X-Path expressions.
- The query results are written to an Excel file. Every other data destination is just conceivable (e.g. database INSERTS). Since several lines are expected, the write mode is “attache rows” in the Excel object.
XML document as destination
As a transfer target, the XML object creates XML code based on the configured X-Path queries.
- The XML code can be transferred further, e.g. to a database. To create an XML file, a script would be required.
For example, it comes from a table:
Author | Title |
------ | ----- |
J.R.R. Tolkien | The Hobbit |
Joanne K. Rowling | Harry Potter and the Philosopher's Stone |
From this, an XML file with the known structure is to be generated.
- Name: “Author”, X-Path expression “/catalog/book/author”
- Name: “Title”, X-Path expression “/catalog/book/title”
For this example, you need two staggered XML objects. The first one will transform the result set into individual XML elements, the second will group the elements into one document:
-
The data records have to be written by query from a database and transferred to the X-Path elements of the first XML transfer object.
-
The XML object generates the XML code. The XML object contains the well-known X-Path expressions:
-
Name: “Author”, X-Path expression “book/author”
-
Name: “Title”, X-Path expression “book/title”
as well as the basic structure
<book>
<author></author>
<title></title>
</book>Important is the property XML output = “Multiple XML documents”, so that each record from the database becomes its own element.
The structure could also be passed dynamically at runtime. E.g. from a text file or from a database.
-
-
The code is passed from the XML element to the X-Path element of the second XML transfer object. This object contains only the XML declaration and the root element:
<?xml version="1.0"?>
<catalog>
</catalog>With the X-Path query / catalog and the property XML output“One XML document”
-
The code can be transferred further. Here, a script transfer object is used to create a file. File path (type: string) and XML content (type: array, string) are passed as input parameters:
public override void Write()
{
StringBuilder stb = new StringBuilder();
stb.AppendLine("<?xml version=\"1.0\"?>");
foreach(string xml in XMLContent)
{
stb.AppendLine(xml.Substring(xml.IndexOf("?>")+2));
}
System.IO.File.WriteAllText(FilePath, stb.ToString());
}