Created
March 7, 2014 23:42
-
-
Save MartijnR/9422577 to your computer and use it in GitHub Desktop.
converts XPath indexed-repeat( ) to a positioned path - indexed-repeat(/path/to/repeat/node, /path/to/repeat, 3) -> /path/to/repeat[position() = 3]/node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Converts an indexed-repeat(node, path, position, path, position, etc) to its native XPath | |
* equivalent using [position() = x] predicates | |
* | |
* @param {string} expr the XPath expression | |
* @return {string} converted XPath expression | |
*/ | |
FormModel.prototype.convertIndexedRepeatToPositionedPath = function( expr ) { | |
var indexedRepeats = expr.match( /(indexed-repeat\s?\([^\)]+\))/g ); | |
if ( !indexedRepeats ) { | |
return expr; | |
} | |
indexedRepeats.forEach( function( indexedRepeat, irIndex ) { | |
var i, positionedPath, | |
params = indexedRepeat.replace( /indexed-repeat\s?\(([^\)]+)\)/g, '$1' ).split( ',' ); | |
if ( params.length % 2 === 1 ) { | |
//trim parameters | |
params.forEach( function( param, index ) { | |
params[ index ] = param.trim(); | |
} ); | |
positionedPath = params[ 0 ]; | |
for ( i = params.length - 1; i > 1; i -= 2 ) { | |
positionedPath = positionedPath.replace( params[ i - 1 ], params[ i - 1 ] + '[position() = ' + params[ i ] + ']' ); | |
} | |
expr = expr.replace( indexedRepeat, positionedPath ); | |
} else { | |
console.error( 'indexed repeat with incorrect number of parameters found', indexedRepeat ); | |
return "'Error with indexed-repeat parameters'"; | |
} | |
} ); | |
return expr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment