xmlGetWhere ( )

Function stats

Average user rating
5.0000
37
155
9999
Support
FileMaker 8.0 +
Date posted
20 December 2008
Last updated
28 July 2009
Version
Recursive function
No

Author Info
 Fabrice

38 functions

Average Rating 4.4

author_avatar



 

Function overview

Prototype

xmlGetWhere  ( _xml;   _tag;   _tagWhere;   _value )


Parameters

_xml  an xml string


_tag  the tag (node) to extract


_tagWhere  the nested tag (node) where the value is required


_value  


Description

Tags:  xml   Text Parsing   Debug  

In an xml structure, extracts the nodes ( _tag ) where a nested tag matches a value.

Examples

Sample input

if _xml is:
<contact><ID>67</ID><Name>Wesson</Name></contact>
<contact><ID>124</ID><Name>Smith</Name></contact>
<contact><ID>780</ID><Name>Wesson</Name></contact>

Example :
// The contact tag(s) that where a Name tag equals Wesson
xmlGetWhere ( _xml ; "contact" ; "Name" ; "Wesson" )


Sample output

<contact><ID>67</ID><Name>Wesson</Name></contact>
<contact><ID>780</ID><Name>Wesson</Name></contact>

 

Function code

/*
xmlGetWhere ( _xml ; _tag ; _tagWhere ; _value )

by Fabrice Nordmann


v.2.2 Jul 2009
- removed empty values
v.2.1 Jan 2009
- even faster. Ready for Sokoban 3D ;-)
v.2.0 Jan 2009
- re-written using CustomList (not recursive), so it shouldn't smoke intel CPUs anymore :)
v.1.0 Dec 2008


In an xml structure, extracts the nodes ( _tag ) where a nested tag matches a value.


Requires :
CustomList () : http://www.fmfunctions.com/fname/customlist


Parameters:
_xml : an xml string
_tag : the tag (node) to extract
_tagWhere : the nested tag (node) where the value is required
_value : what needs to be found in _tagWhere for _tag to be extracted


Example:
if _xml is:
<contact><ID>67</ID><Name>Wesson</Name></contact>
<contact><ID>124</ID><Name>Smith</Name></contact>
<contact><ID>780</ID><Name>Wesson</Name></contact>

------------------------

// The contact tag(s) that where a Name tag equals Wesson

xmlGetWhere ( _xml ; "contact" ; "Name" ; "Wesson" ) =
"<contact><ID>67</ID><Name>Wesson</Name></contact>
<contact><ID>780</ID><Name>Wesson</Name></contact>"

------------------------
*/

Let ([
$cf_xmlw_value = Substitute ( _value ; ¶ ; "#CR#" )
; $tagWhere = _tagwhere
; $cf_tagWhereWdata = "<" & $tagwhere & ">" & $cf_xmlw_value & "</" & $tagwhere & ">"
; $cf_matchString = "#M#A#T#C#H#"
; $cf_xmlw_xml = Substitute ( _xml ; [ "¶</" ; "#CLOSETAG#" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; [ "¶<" ; "<" ] ; ["#CLOSETAG#" ; "¶</" ] ; [ ¶ ; "#CR#" ] ; [ "<" & _tag ; "¶<" & _tag ] ; [ "¶</" & _tag & ">" ; "</" & _tag ] ; [ $cf_tagWhereWdata ; $cf_matchString ] )
];
Substitute ( CustomList ( 1 ; ValueCount ( $cf_xmlw_xml ) ; "let ([ $cf_xmlw_v = getvalue ( $cf_xmlw_xml ; [n] ) ]; case ( patterncount ( $cf_xmlw_v ; $cf_matchString ) ; $cf_xmlw_v ))" ) ; [ $cf_matchString ; $cf_tagWhereWdata ] ; [ "#CR#" ; ¶ ] )
)



// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/155

    Prototype: xmlGetWhere( _xml; _tag; _tagWhere; _value )
    Function Author: Fabrice (http://www.fmfunctions.com/mid/37)
    Last updated: 28 July 2009
    Version: 2.6

*/
// ===================================

 

Comments

Fabrice
20 December 2008



I would be glad to have comments on this one, especially about the documentation, parameter names or the function name (I'm not very happy with those).
  General comment
Michael W.
13 February 2009



This function is invaluable in a project that I am doing. With this one and the GetXMLNode function that I wrote it took care of handling the XML that I needed to parse very nicely. Thanks for the great work!
  General comment

 

 


Login or register to comment

Create a new account with fmcustomfunctions.com or login to post a comment.