Search code examples
xmlshellapplescriptxmllint

How can I insert separator characters in a xmllint query that returns several results


Hello all you clever people :-) I'm calling a shell script from AppleScript to fetch values from an xml file. The file (simplified!) looks like this :

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ProductHints spec="1.0.16">
  <Product version="4">
    <Name>The Product Name</Name>
    <Company>The Company Name</Company>
    <PRODID>A123</PRODID>
  </Product>
</ProductHints>

My AppleScript looks like this :

set thePath to "/Path/to/my/file.xml"
set theResult to do shell script "xmllint " & quoted form of thepath & " --xpath '/ProductHints/Product/Name/text() | /ProductHints/Product/PRODID/text() | /ProductHints/Product/Company/text()'"
display dialog theResult

As a result, this displays : The Product NameThe Company NameA123

This is indeed the required information, but not much use as it is ! I would like the result to be either tab separated or comma separated (tab would be ideal, but I could work with comma !) for example :

The Product Name **Tab** The Company Name **Tab** A123

or

The Product Name, The Company Name, A123

I'm sure there's a simple solution to place something in between each item, but I've tried various things to no avail !

Could any kind soul help me out here please. Thanks in advance.


Solution

  • Ooooops ! That code block was a bit of a mess. I'll try again :

    set thepath to "/Path/to/My/file name.xml"
     set theScript to "xmllint --xpath \"concat(/ProductHints/Product/Name/text(),',',/ProductHints/Product/PRODID/text(),',',/ProductHints/Product/Company/text())\" " & quoted form of thepath
     set theResult to do shell script theScript without altering line endings
    display dialog theResult