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
1-more-thing
http://www.1-more-thing.com
v.3 Dec 2010
- completely rewritten
- much faster
- not case sensitive
- solved an issue where nested nodes had their parent node name in their name (like <CONTACT><CONTACT NAME></CONTACT NAME></CONTACT>)
- returns a node if any of the 'tagwhere' equals _value (was taking only the first one into account)
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:
<address book>
<contact><ID>67</ID><Name>Wesson</Name></contact>
<contact><ID>124</ID><Name>Smith</Name></contact>
<contact><ID>780</ID><Name>Wesson</Name></contact>
</address book>
------------------------
// 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>"
------------------------
*/
Case ( IsEmpty ( $cf_i ) ;
Let ([
$cf_i = 0 ;
$cf_xml = Substitute ( _xml ; ¶ ; "#CR#" ) ;
$cf_xmlw_value = Substitute ( _value ; ¶ ; "#CR#" ) ;
$cf_tag = "<" & _tag & ">" ;
$cf_length = Length ( $cf_tag ) ;
$cf_tagClosing = "</" & _tag & ">" ;
$cf_tagCount = PatternCount ( $cf_xml ; $cf_tag ) ;
$cf_tagWhere = _tagwhere ;
$cf_tagWhereWdata = "<" & $cf_tagWhere & ">" & $cf_xmlw_value & "</" & $cf_tagWhere & ">"
];
xmlGetWhere ( $cf_xml ; "" ; "" ; "" )
) ;
Let ( $cf_i = $cf_i + 1 ; "" ) &
Case (
$cf_i <= $cf_tagCount ;
// modify the xml so the opening tag is marked
xmlGetWhere ( Replace ( _xml ; Position ( _xml ; $cf_tag ; 1 ; $cf_i ) ; 0 ; "#T#A#G#" ) ; "" ; "" ; "" ) ;
$cf_i <= $cf_tagCount * 2 ;
//do the same with closing tags
xmlGetWhere ( Replace ( _xml ; Position ( _xml ; $cf_tagClosing ; 1 ; $cf_i / 2 ) + Length ( $cf_tagClosing ) ; 0 ; "#T#A#G#" ) ; "" ; "" ; "" ) ;
// extract the matching values
Let ([
$cf_xml = Substitute ( _xml ; "#T#A#G#" ; ¶ ) ;
_result = //_xml
Substitute (
CustomList ( 1 ; ValueCount ( $cf_xml ) ; "let ( x = getvalue ( $cf_xml ; [n] ) ; case ( left ( x ; $cf_length ) = $cf_tag and PatternCount ( x ; $cf_tagWhereWdata ) ; x ))" ) ;
"#CR#" ; ¶
) ;
// clean the mess
$cf_i = "" ;
$cf_xml = "" ;
$cf_xmlw_value = "" ;
$cf_tag = "" ;
$cf_length = "" ;
$cf_tagClosing = "" ;
$cf_tagCount = "" ;
$cf_tagWhere = "" ;
$cf_tagWhereWdata = ""
];
_result
)
)
)
// ===================================
/*
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: 10 December 2010
Version: 3.1
*/
// ===================================
/*__LITBR__xmlGetWhere ( _xml ; _tag ; _tagWhere ; _value )__LITBR____LITBR__by Fabrice Nordmann__LITBR____LITBR__1-more-thing__LITBR____LITBR__http://www.1-more-thing.com__LITBR____LITBR__v.3 Dec 2010__LITBR__ - completely rewritten__LITBR__ - much faster__LITBR__ - not case sensitive__LITBR__ - solved an issue where nested nodes had their parent node name in their name (like <CONTACT><CONTACT NAME></CONTACT NAME></CONTACT>)__LITBR__ - returns a node if any of the 'tagwhere' equals _value (was taking only the first one into account)__LITBR__v.2.2 Jul 2009__LITBR__ - removed empty values__LITBR__v.2.1 Jan 2009__LITBR__ - even faster. Ready for Sokoban 3D ;-)__LITBR__v.2.0 Jan 2009__LITBR__ - re-written using CustomList (not recursive), so it shouldn't smoke intel CPUs anymore :)__LITBR__v.1.0 Dec 2008__LITBR____LITBR____LITBR__In an xml structure, extracts the nodes ( _tag ) where a nested tag matches a value.__LITBR____LITBR____LITBR__Requires :__LITBR__CustomList () : http://www.fmfunctions.com/fname/customlist __LITBR____LITBR____LITBR__Parameters:__LITBR___xml : an xml string__LITBR___tag : the tag (node) to extract__LITBR___tagWhere : the nested tag (node) where the value is required__LITBR___value : what needs to be found in _tagWhere for _tag to be extracted__LITBR____LITBR____LITBR__Example:__LITBR__if _xml is:__LITBR__<address book>__LITBR__<contact><ID>67</ID><Name>Wesson</Name></contact>__LITBR__<contact><ID>124</ID><Name>Smith</Name></contact>__LITBR__<contact><ID>780</ID><Name>Wesson</Name></contact>__LITBR__</address book>__LITBR____LITBR__------------------------__LITBR____LITBR__// The contact tag(s) that where a Name tag equals Wesson__LITBR____LITBR__xmlGetWhere ( _xml ; "contact" ; "Name" ; "Wesson" ) =__LITBR__"<contact><ID>67</ID><Name>Wesson</Name></contact>__LITBR__<contact><ID>780</ID><Name>Wesson</Name></contact>"__LITBR____LITBR__------------------------__LITBR__*/__LITBR____LITBR__Case ( IsEmpty ( $cf_i ) ;__LITBR__ Let ([__LITBR__ $cf_i = 0 ;__LITBR__ $cf_xml = Substitute ( _xml ; ¶ ; "#CR#" ) ;__LITBR__ $cf_xmlw_value = Substitute ( _value ; ¶ ; "#CR#" ) ;__LITBR__ $cf_tag = "<" & _tag & ">" ;__LITBR__ $cf_length = Length ( $cf_tag ) ;__LITBR__ $cf_tagClosing = "</" & _tag & ">" ;__LITBR__ $cf_tagCount = PatternCount ( $cf_xml ; $cf_tag ) ;__LITBR__ $cf_tagWhere = _tagwhere ;__LITBR__ $cf_tagWhereWdata = "<" & $cf_tagWhere & ">" & $cf_xmlw_value & "</" & $cf_tagWhere & ">"__LITBR__ ];__LITBR__ xmlGetWhere ( $cf_xml ; "" ; "" ; "" )__LITBR__ ) ;__LITBR__ Let ( $cf_i = $cf_i + 1 ; "" ) &__LITBR__ Case (__LITBR__ $cf_i <= $cf_tagCount ;__LITBR__ // modify the xml so the opening tag is marked__LITBR__ xmlGetWhere ( Replace ( _xml ; Position ( _xml ; $cf_tag ; 1 ; $cf_i ) ; 0 ; "#T#A#G#" ) ; "" ; "" ; "" ) ;__LITBR__ $cf_i <= $cf_tagCount * 2 ;__LITBR__ //do the same with closing tags__LITBR__ xmlGetWhere ( Replace ( _xml ; Position ( _xml ; $cf_tagClosing ; 1 ; $cf_i / 2 ) + Length ( $cf_tagClosing ) ; 0 ; "#T#A#G#" ) ; "" ; "" ; "" ) ;__LITBR____LITBR__ // extract the matching values__LITBR__ Let ([__LITBR__ $cf_xml = Substitute ( _xml ; "#T#A#G#" ; ¶ ) ;__LITBR__ _result = //_xml__LITBR__ Substitute (__LITBR__ CustomList ( 1 ; ValueCount ( $cf_xml ) ; "let ( x = getvalue ( $cf_xml ; [n] ) ; case ( left ( x ; $cf_length ) = $cf_tag and PatternCount ( x ; $cf_tagWhereWdata ) ; x ))" ) ;__LITBR____LITBR__ "#CR#" ; ¶__LITBR__ ) ;__LITBR____LITBR__ // clean the mess__LITBR__ $cf_i = "" ;__LITBR__ $cf_xml = "" ;__LITBR__ $cf_xmlw_value = "" ;__LITBR__ $cf_tag = "" ;__LITBR__ $cf_length = "" ;__LITBR____LITBR__ $cf_tagClosing = "" ;__LITBR__ $cf_tagCount = "" ;__LITBR__ $cf_tagWhere = "" ;__LITBR__ $cf_tagWhereWdata = "" __LITBR__ ];__LITBR__ _result __LITBR__ )__LITBR____LITBR__ )__LITBR__)__LITBR____LITBR__// ===================================__LITBR__/*__LITBR____LITBR__ This function is published on FileMaker Custom Functions__LITBR__ to check for updates and provide feedback and bug reports__LITBR__ please visit http://www.fmfunctions.com/fid/155__LITBR____LITBR__ Prototype: xmlGetWhere( _xml; _tag; _tagWhere; _value )__LITBR__ Function Author: Fabrice (http://www.fmfunctions.com/mid/37)__LITBR__ Last updated: 10 December 2010__LITBR__ Version: 3.1__LITBR____LITBR__*/__LITBR__// ===================================
Login or register to comment
Create a new account with fmcustomfunctions.com or login to post a comment.
Comments
20 December 2008
14 February 2009
18 August 2010
Thanks again.