I am getting my hands on BizTalk and VS. My input schema looks something similar to this.
<root>
<order>
<orderid>
<orderdate>
...
...
and the output schema
<order>
<header:sequence>
<element name="orderid">
<element name="orderdate">
...
...
</header:sequence>
In short, in output, the header is a sequence of complex types and individual nodes in the source are enumerated as the sequence in the output.
How do we solve this in Visual Studio?
What you need to do is having a looping functoid that goes from each of the element being mapped and to the repeating destination element. And then two links from the source elements the first that is a standard link Copy text value, the second that goes to the name attribute, for which you change the link to Copy name.
Input
<root>
<order>
<orderid>1234567890</orderid>
<orderdate>2020-01-28</orderdate>
</order>
</root>
Output
<order>
<header>
<element name="orderid">1234567890</element>
<element name="orderdate">2020-01-28</element>
</header>
</order>
Note: You can change the order of what is output by using the reorder inputs in the Configure Looping Functoid.
Related
Good night, friends!
Lets suppose we have a xml with 30 items like that and I want to get just the name and url from an item where name contains the word: Richard.
<channel>
<item>
<name>Brian</name>
<lastname>Connor</lastname>
<age>40</age>
<enclosure url="http://www.brian.com"/>
</item>
<item>
<name>Richard</name>
<lastname>Wendell</lastname>
<age>38</age>
<enclosure url="http://www.richard.com"/>
</item>
</channel>
How can I do that using XPath?
I tried:
"//channel/item[name[contains(text(),'Richard')]]" but it returns just the name and I don't know how to select the url information together.
Please excuse my english!
Your approach does not work because you are selecting a sub tree (in this case an item) of the XML tree which contains more information than you want. If you want just a subset of the attributes in ONE xpath expression you have to select them separately and then concatenate them adequately, e.g.
concat('name=', //channel/item[contains(name, 'Richard')]/name, ' url=', //channel/item[contains(name, 'Richard')]/enclosure/#url)
The example will allow you to alter the additional formatting easily.
By the way: your XML input was malformatted. I corrected this.
first post ever, have done lots of searching but cannot find an answer specific enough or more importantly, relevant enough. Note that I am a business analyst, not a developer, so I may be missing some understanding here.
We produce XML that we then process to produce a report. Where data can be represented by a table, the XML contains details for the table title (ELEMENT_HEADING), table header row (PROMPTS), then repeating nodes representing the rows (DATA) and columns (VALUES).
The problem I am facing is I need to sort the DATA node based on a text value of the node where the node has a specific attribute value.
In the sample XML provided below, I need to sort the DATA node based on the VALUE text value for the attribute #pic='TRORGPCNT' in ascending order i.e the DATA node with TRORGPCNT of 10 should appear before the DATA node with 90. Then when the report is produced the table rows are in ascending percentage order.
I hope have explained myself clearly enough :)
Any tips on how I might accomplish this?
Sample XML:
<PROPOSAL_ELEMENT multi="Y" pec="TEACHRESP" elem_mandatory="N" elem_visible="Y">
<ELEMENT_HEADING pec="TEACHRESP">Teaching Responsibility</ELEMENT_HEADING>
<PROMPTS>
<PROMPT pic="TRORGUN" item_mandatory="Y" item_visible="Y">Faculty or School with teaching responsibility</PROMPT>
<PROMPT pic="TRORGPCNT" item_mandatory="Y" item_visible="Y">Teaching responsibility %</PROMPT>
</PROMPTS>
<DATA elem_mandatory="N" elem_visible="Y" delete_ind="N">
<VALUES>
<VALUE pic="TRORGUN" item_mandatory="Y" item_visible="Y" item_description="FACULTY OF NURSING AND HEALTH" display_in_summary_tab="Y" summary_order="">FACULTY OF NURSING AND HEALTH</VALUE>
<VALUE pic="TRORGPCNT" item_mandatory="Y" item_visible="Y" item_description="" display_in_summary_tab="Y" summary_order="">90</VALUE>
</VALUES>
</DATA>
<DATA elem_mandatory="N" elem_visible="Y" delete_ind="N">
<VALUES>
<VALUE pic="TRORGUN" item_mandatory="Y" item_visible="Y" item_description="FACULTY OF ARTS" display_in_summary_tab="Y" summary_order="">FACULTY OF ARTS</VALUE>
<VALUE pic="TRORGPCNT" item_mandatory="Y" item_visible="Y" item_description="" display_in_summary_tab="Y" summary_order="">10</VALUE>
</VALUES>
</DATA>
Sorting in XSLT is accomplished using the xsl:sort instruction, which must appear as the first child of the for-each or apply-templates that selects the nodes you want to sort. If you're selecting the set of DATA element nodes then an appropriate sorting instruction would be
<xsl:sort select="VALUES/VALUE[#pic='TRORGPCNT']"
data-type="number" />
i have an understanding-problem using Microsoft XML Core Services 6.0 (MSXML) with XPath-expressions.
I´ve broken down the problem to the most simple case. So let´s take the following XML-File:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element name="E1A1">
<subEle value="1a"/>
<subEle value="1b"/>
<subEle value="1c"/>
</element>
<element name="E2A1">
<subEle value="2a"/>
<subEle value="2b"/>
<subEle value="3b"/>
</element>
<element name="E3A1">
<subEle value="3a"/>
<subEle value="3b"/>
<subEle value="3c"/>
</element>
</root>
I want to get the "value"-attribues per "element". I will use pseudo-code to describe
my problem and i will focus on the important things, so i will not write how i initialize
the Msxml2.DOMDocument variable etc. .
First, i get all "element"-nodes that have a name-attribute:
oNodeList = oDom.selectNodes("//element[#name]")
The result of the selectNodes-statement is a nodelist, where i access the items node by node
in a for-loop. In this loop, i execute another selectNodes-statement, that gives me (at least i thought so)
the "subEle"s for each "element":
for i from 1 to oNodeList.length
oNodeMain = oNodeList.nextNode()
oNodeResList = oNodeMain.selectNodes("//subEle")
msgInfo("n items", oNodeResList.length)
endFor
And here comes the problem: the selectNodes-statement in the loops seems to have ALL "subEle"s
in scope; the messagebox pops up three times, telling me the length of the nodelist is 9.
I would have expected that it pops up 3 times, telling me each time that the nodelist has a length of 3 (because
every "element" has exactly 3 "subEle"s), since i´m doing the selectNodes-statement on "oNodeMain",
which gets the nextNode in each loop. Maybe i just need to modify XPath-expression in the loop and
don´t use the "//", because it works then, but i have no idea why.
The program i use for this is Paradox 11 and i use MSXML by OLE.
Is this behaviour "normal", where is my misunderstanding? Any suggestions on how to achieve what i´m
trying are welcome.
Don't use an absolute path starting with /, instead use a relative path i.e. oNodeMain.selectNodes("subEle") selects all subEle child elements of oNodeMain and oNodeMain.selectNodes(".//subEle") selects all descendant subEle elements of oNodeMain.
Your path starting with // searches from the root node (also called document node).
I want to use XPath to select the sub tree containing the <name>-tag with "ABC" and not the other one from the following xml. Is this possible? And as a minor question, which keywords would I use to find something like that over Google (e.g. for selecting the sub tree by an attribute I would have the terminology for)?
<root>
<operation>
<name>ABC</name>
<description>Description 1</description>
</operation>
<operation>
<name>DEF</name>
<description>Description 2</description>
</operation>
</root>
Use:
/*/operation[name='ABC']
For your second question: I strongly recommend not to rely on online sources (there are some that aren't so good) but to read a good book on XPath.
See some resources listed here:
https://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589
For your first question, I think a more accurate way to do it would be://operation[./name[text()='ABC']].And according to this , we can also make it://operation[./name[text()[.='ABC']]]
I can't navigate the XML doc programmatically and I need an one-line XPath solution for reasons I describe at the end.
I am working with an XML schema that looks something like the one below. (This is something I have to use as-is.)
<Root>
<!-- Child 1 -->
<Child>
<Name>Joe</Name>
<Age>12</Age>
</Child>
<!-- Child 2 -->
<Child>
<Name>Mike</Name>
<Age>25</Age>
</Child>
<!-- Child 3 -->
<Child>
<Name>Jane</Name>
<Age>20</Age>
</Child>
</Root>
Assuming I'm already at the "Joe" node (i.e. the Name element inside Child 1), I need to define an XPath query that will "wrap" that node as follows:
<Root>
<!-- Child 1 -->
<Child>
<Name>Joe</Name>
<Age>12</Age>
</Child>
</Root>
I've tried various combinations of ancestor, string-join, concat, etc., but can't seem to find the solution that "wraps" the element correctly. (The way I was using ancestor was returning all Child nodes, for example, which is not what I need.)
Some other considerations:
The solution has to be a one-line XPath query, if that's possible (for reasons given below).
It has to be generic enough to work for any Child element (i.e., it can't assume that I'm always at the first or second or third child, for example).
From the example above, you can see that I don't actually need the actual Root node per-se, just its tag (i.e. I don't want all Child nodes under it). However, I do need the actual Child node (so that I get the Name and Age).
NOTE: For what it's worth, I can't actually navigate the XML programmatically. I am using a library (whose code I cannot change) in which I have to define everything in terms of one-line XPath queries within a configuration file. It will essentially navigate through all of the Name elements, so my solution has to work from that point.
XPath is a query language.
This, among other things means that the evaluation of an XPath expression never modifies the XML document.
So, the answer is: Modifying an XML document or creating a new document cannot be done using only XPath.
Such transformations are very easy and natural to specify with XSLT.