Sponsored Content
Top Forums Shell Programming and Scripting How to get a field from an XML? Post 302492194 by fpmurphy on Sunday 30th of January 2011 10:22:51 AM
Old 01-30-2011
First of all, what you are showing us in not an XML document. It is a text document using XML-like tags. A valid XML document (example.xml) has a single root, i.e.
Code:
<docs>
  <doc>
     <str name="account_id">1111</str>
     <str name="prd_id">DHEP155EK</str>
  </doc>
  <doc>
     <str name="account_id">6666</str>
     <str name="prd_id">394531662</str>
  </doc>
</docs>

If you have a valid document, you can use an XSL stylesheet to quickly contain the output you want.

For example, here is a stylesheet (example.xsl):
Code:
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:apply-templates select="docs"/>
   </xsl:template>

   <xsl:template match="docs">
      <xsl:apply-templates select="doc"/>
   </xsl:template>

   <xsl:template match="doc">
      <xsl:value-of select="./str[@name='account_id']" />|<xsl:value-of select="./str[@name='prd_id']" />
<xsl:text>
</xsl:text>
   </xsl:template>

</xsl:stylesheet>

which when used with a XSLT engine such as xsltproc produces the output you want:
Code:
$ xsltproc example.xsl example.xml
1111|DHEP155EK
6666|394531662

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How To Find Length of a Field in XML File

Hi I have a xml file with below data...have to find the length of the filedvalues... <?xml version="1.0" encoding="ISO-8859-15" standalone="no"?><abc xmlns:xsi="http://www.w3.org/2000/XMLSchem... (3 Replies)
Discussion started by: naughty21
3 Replies

2. Programming

Extracting Field values for XML file

i have an input file of XML type with data like <nx-charging:additional-parameter name="NX_INTERNATIONALIZED_CLID" value="919427960829"/><nx-charging:finalStatus>RESPONSE , Not/Applicable , OK</nx-charging:finalStatus></nx-charging:process> i want to extract data such that i get the output... (3 Replies)
Discussion started by: junaid.nehvi
3 Replies

3. Shell Programming and Scripting

Help needed XML Field Extraction

I had an immediate work to sort out the error code and error message which are associated within the log. But here im facing an problem to extract 3 different fields from the XML log can some one please help. I tried using different script including awk & nawk, but not getting the desired output. ... (18 Replies)
Discussion started by: raghunsi
18 Replies

4. UNIX for Dummies Questions & Answers

Extract Field Value from XML file

Hi, Within a UNIX shell script I need to extract a value from an XML field. The field will contain different values but will always be 6 digits in length. E.g.: <provider-id>999999</provider-id> I've tried various ways but no luck. Any ideas how I might get the provider id (in this case... (2 Replies)
Discussion started by: pnclayt11
2 Replies

5. Shell Programming and Scripting

How do I extract the third field in an xml entry?

I have the following XML entry - I need to extract the contents of the third defined variable, Asset_Name. <AMS Asset_Class="package" Asset_ID="XXXXXXXXXXXXXXXXXXXX" Asset_Name="MOVIE_XXXXXXXXXXX_XXXXXXXX" Creation_Date="2011-09-13" Description="test asset package" Product="MOD"... (4 Replies)
Discussion started by: hrr_mute
4 Replies

6. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

7. Shell Programming and Scripting

Shell Command to compare two xml lines while ignoring xml tags

I've got two different files and want to compare them. File 1 : HTML Code: <response ticketId="944" type="getQueryResults"><status>COMPLETE</status><description>Query results fetched successfully</description><recordSet totalCount="1" type="sms_records"><record... (1 Reply)
Discussion started by: Shaishav Shah
1 Replies

8. Red Hat

How to get a particular field from xml file?

i have a xml file and in that input file path is given. how to fetch that input file path using shell script. (4 Replies)
Discussion started by: ramsavi
4 Replies

9. Shell Programming and Scripting

Cgi to dump xml data from form input field

Hi All, I am trying to write a shell script which takes parse the web form find the input field and dump the data of that field into one xml file. The form looks like, <input type="button" id="btnSave" value="Save" onclick="saveXmlData()"/> <form name="submitForm"... (1 Reply)
Discussion started by: jdp
1 Replies

10. Programming

Field delimited data to XML

Hi, We need to produce a XML file based on a record/field delimited data file. At this point we could just script something out but I would like to ask the community what would be the best choice of programming language to do this, in terms of performance of execution, and in terms of complexity... (9 Replies)
Discussion started by: Indalecio
9 Replies
XSLT_SET_ERROR_HANDLER(3)						 1						 XSLT_SET_ERROR_HANDLER(3)

xslt_set_error_handler - Set an error handler for aXSLTprocessor

SYNOPSIS
void xslt_set_error_handler (resource $xh, mixed $handler) DESCRIPTION
Set an error handler function for the XSLT processor given by $xh, this function will be called whenever an error occurs in the XSLT transformation (this function is also called for notices). PARAMETERS
o $ xh -The XSLT processor link identifier, created with xslt_create(3). o $handler - The user function needs to accept four parameters: the XSLT processor, the error level, the error code and an array of messages. The function can be shown as: error_handler (resource $xh, int $error_level, int $error_code, array $messages) RETURN VALUES
No value is returned. EXAMPLES
Example #1 xslt_set_error_handler(3) Example <?php // Our XSLT error handler function xslt_error_handler($handler, $errno, $level, $info) { // for now, let's just see the arguments var_dump(func_get_args()); } // XML content : $xml='<?xml version="1.0"?> <para> oops, I misspelled the closing tag </pata>'; // XSL content : $xsl='<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <strong><xsl:value-of select="para"/></strong> </xsl:template> </xsl:stylesheet>'; $xh = xslt_create(); xslt_set_error_handler($xh, "xslt_error_handler"); echo xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, array("/_xml" => $xml, "/_xsl" => $xsl)); ?> The above example will output something similar to: array(4) { [0]=> resource(1) of type (XSLT Processor) [1]=> int(3) [2]=> int(0) [3]=> array(6) { ["msgtype"]=> string(5) "error" ["code"]=> string(1) "2" ["module"]=> string(9) "Sablotron" ["URI"]=> string(9) "arg:/_xml" ["line"]=> string(1) "4" ["msg"]=> string(34) "XML parser error 7: mismatched tag" } } SEE ALSO
xslt_set_object(3) if you want to use an object method as handler. PHP Documentation Group XSLT_SET_ERROR_HANDLER(3)
All times are GMT -4. The time now is 01:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy