From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nicolas Richard Newsgroups: gmane.emacs.help Subject: Re: Troubles in Regular Expression Paradise Date: Wed, 14 May 2014 14:40:05 +0200 Message-ID: <87oaz0ilei.fsf@geodiff-mac3.ulb.ac.be> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1400071179 21283 80.91.229.3 (14 May 2014 12:39:39 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 14 May 2014 12:39:39 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed May 14 14:39:33 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WkYT7-0004C6-JB for geh-help-gnu-emacs@m.gmane.org; Wed, 14 May 2014 14:39:33 +0200 Original-Received: from localhost ([::1]:51408 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WkYT7-0005RO-6X for geh-help-gnu-emacs@m.gmane.org; Wed, 14 May 2014 08:39:33 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41756) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WkYSq-0005QW-Qe for help-gnu-emacs@gnu.org; Wed, 14 May 2014 08:39:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WkYSj-0007Eg-SF for help-gnu-emacs@gnu.org; Wed, 14 May 2014 08:39:16 -0400 Original-Received: from mxin.ulb.ac.be ([164.15.128.112]:28992) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WkYSj-0007EU-M5 for help-gnu-emacs@gnu.org; Wed, 14 May 2014 08:39:09 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq4EAIxic1OkD4Xx/2dsb2JhbABZg1WsIwaaHQGBM3SCJQEBBAF+CwshJQ8BBFyILAEMCA2sTJ0RAYcrEwSFVIF/hwIWhCoEoCOMQYM4Ow Original-Received: from mathsrv4.ulb.ac.be (HELO geodiff-mac3.ulb.ac.be) ([164.15.133.241]) by smtp.ulb.ac.be with ESMTP; 14 May 2014 14:39:08 +0200 In-Reply-To: (Len Blanks's message of "Tue, 13 May 2014 21:18:58 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.91 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 164.15.128.112 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:97643 Archived-At: Len Blanks writes: > I'm trying to parse an xml file containing information for a song "currently playing" > on my iTunes - specifically the artist, name of the tune and the CD in appears on. > and here is a function I had hoped would strip the relevant fields and return a string in > the form: "_artist_'s _title_ from the CD _album_" to be inserted in a X-NOW-PLAYING: > header in emails and usenet posts: The regexp question was answered, so I allow myself to mention libxml-parse-xml-region instead of regexps for parsing xml. First eval: (setq yftest (libxml-parse-html-region (point-min) (point-max))) in a buffer which holds your file. Then you can get away with: (caddr (assoc 'title (caddr yftest))) (caddr (assoc 'artist (caddr yftest))) (caddr (assoc 'album (caddr yftest))) to get the title, artist and album respectively. As a side note, I wrote some elisp for using the kind of data that libxml-parse-html-region spits out, so here's my way of solving your problem with my code : (require 'tree-html) ;; https://github.com/YoungFrog/tree-html/blob/master/tree-html.el (defun yf/get-value-for-sole-subtree-with-given-tag (tree tag) "Assume there's exactly one XML element with given TAG in TREE, and return its associated value." (yf/tree-html-get-value (yf/tree-html-get-sole-element (yf/tree-html-select tree (lambda (tree) (eq tag (yf/tree-html-get-tag tree))))))) (format "%s's %s from the CD %s" (yf/get-value-for-sole-subtree-with-given-tag yftest 'artist) (yf/get-value-for-sole-subtree-with-given-tag yftest 'title) (yf/get-value-for-sole-subtree-with-given-tag yftest 'album)) (Yes, I'm *that* bad at naming things.) HTH, -- Nico.