GetNthCdataContents ( )

Function stats

Average user rating
496
388
9999
Support
FileMaker 10.0 +
Date posted
01 October 2015
Last updated
02 October 2015
Version
Recursive function
No

Author Info
 Cristos L-C

7 functions

Average Rating None

author_avatar



 

Function overview

Prototype

GetNthCdataContents  ( xmlToSearch;   n )


Parameters

xmlToSearch  The XML within which to find CDATA.


n  The instance number of the CDATA you want to extract.


Description

Tags:  XML   CDATA  

Gets the contents of the nth CDATA instance within an XML blob.

Examples

Sample input

GetNthCdataContents ( "<DATA><![CDATA[<idStaff>T00183</idStaff><modified>2015-07-29 11:50:31</modified>]]></DATA> <DATA><![CDATA[<idStaff>T00184</idStaff><modified>2015-07-29 11:50:31</modified>]]></DATA>" ; 2 )


Sample output

"<idStaff>T00184</idStaff><modified>2015-07-29 11:50:31</modified>"

 

Function code

/*
    GetNthCdataContents ( xmlToSearch ; n )

PURPOSE:
    Gets the contents of the nth CDATA instance within an XML blob.

RETURNS:
    Text string, of the data contained between the opening and closing CDATA tags.

EXAMPLE:
    CALL:
        GetNthCdataContents ( "<DATA><![CDATA[<idStaff>T00183</idStaff><modified>2015-07-29 11:50:31</modified>]]></DATA> <DATA><![CDATA[<idStaff>T00184</idStaff><modified>2015-07-29 11:50:31</modified>]]></DATA>" ; 2 )
    RESPONSE:
        "<idStaff>T00184</idStaff><modified>2015-07-29 11:50:31</modified>"

CHANGELOG:
    ## 2015-10-01 8:20:38 PM EDT cristos@inresonance.com
    - adjusted to better-handle out-of-bounds numbers. (Thanks to Bruce, Redmond WA, for pointing it out. | http://www.briandunning.com/cf/1817)
    ## 2015-09-30 11:18 EST cristos@inresonance.com
    - Created
*/


Let (
/* First, get the bounds of the CDATA element's opening and closing tags. */
[    ~n = n
;    ~xml = xmlToSearch

;    ~cdataPos0Text = "<![CDATA["
;    ~cdataPos1Text = "]]>"


;    ~minN = 1
;    ~maxN = PatternCount ( ~xml ; ~cdataPos0Text )


;    ~cdataPos0Length = Length ( ~cdataPos0Text )
;    ~cdataPos0 = Position ( ~xml ; ~cdataPos0Text ; 0 ; ~n )
;    ~cdataPos1 = Position ( ~xml ; ~cdataPos1Text ; ~cdataPos0 ; 1 )

/* Then get the bounds of the value inside the CDATA bounds. */
;    ~valuePos0 = ~cdataPos0 + ~cdataPos0Length
;    ~valuePos1 = ~cdataPos1
;    ~valueLength = ~valuePos1 - ~valuePos0

/* Last, get the value itself. */
;    ~value = Case
    (    ~n < ~minN
        or ~n > ~maxN
    ;        "?"
        /* ELSE */
    ;        Middle ( ~xml ; ~valuePos0 ; ~valueLength )
    )
]
;    ~value
)

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

    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/388

    Prototype: GetNthCdataContents( xmlToSearch; n )
    Function Author: Cristos L-C (http://www.fmfunctions.com/mid/496)
    Last updated: 02 October 2015
    Version: 2.0

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

 

 

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (31)
Date  (28)
XML  (26)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Interface  (15)
Layout  (15)
Text  (14)
Variables  (12)
Layout Objects  (11)
Filter  (11)
Design  (10)
Array  (7)