I am using this line of code to get two data entries from an XML file
perl xmlPerl.pl zbxml.xml "//zabbix_export/templates/template/items/item/name/text() | //zabbix_export/templates/template/items/item/description/text()"
Which takes the data, and displays it vertically. For example:
- name1
- description1
- name2
- description2
I used this in c# and had some code so that it would display like this
- name1 – description1
- name2 – description2
- name3 – (blank since there
isnt a description)
there were even some blanks in description. Here is the c# code, since it may help.
XPathExpression expr;
expr = nav.Compile("/zabbix_export/templates/template/items/item/name | /zabbix_export/templates/template/items/item/description");
XPathNodeIterator iterator = nav.Select(expr);
//Iterate on the node set
List<string> listBox1 = new List<string>();
listBox1.Clear();
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
// nav2.Value;
listBox1.Add(nav2.Value);
Console.Write(nav2.Value);
iterator.MoveNext();
nav2 = iterator.Current.Clone();
Console.Write("-" + nav2.Value + "\n");
Well, I am having to switch it to Perl now, and I am not sure if I should try and find some Perl code to do what I need, or if this can be done in XPath? I tried looking at some w3 tutorials, but didn’t find what I was looking for.
Thanks!
edit –
would I need to edit this part of my xmlPerl.pl
# print each node in the list
foreach my $node ( $nodeset->get_nodelist ) {
print XML::XPath::XMLParser::as_string( $node ) . "\n";
}
It cannot be done with an XPath. It can be done with an XSL transformation:
A simple Perl script that applies this XSLT will do the trick – see this for example (or any other command-line utility that applies an XSLT for that matter – like msxsl.exe)