Skip to content

Sync

Synchronizes the nodes at the specified XPath with the input nodes, and returns an array of the synchronized nodes.

1
myXml.Sync(xpath: string, input: Array<Xml>, hash: (node: Xml) => string):Array<Xml>

Synchronizes the nodes at the specified XPath with the input nodes, and returns an array of the synchronized nodes.

1
myXml.Sync(xpath: string, input: Array<Xml>, hash: (node: Xml) => string, removedNodes: Array<Xml>):Array<Xml>

Parameters

string xpath
    XPath of the node to synchronize.

Array<Xml> input
    Array of Xml nodes to synchronize with.

(node: Xml) => string hash
    Hash function to compare nodes. If omitted, the node instance is used for comparison.

Array<Xml> removedNodes
    Optional array to add removed nodes to.

Returns

An array of synchronized Xml nodes. An array of synchronized Xml nodes.

Remarks

Synchronization is based on the input nodes and performs the following actions at the specified XPath:
- Node comparison is performed using the specified hash function, which returns a unique identifier for each node. If no hash function is specified, the node instance is used for comparison.
- Nodes that exist in the input array but not in the current node are automatically created in the current node.
- Existing nodes in both the input array and the current node are copied.
- Nodes that exist in the current node but not in the input array are removed from the current node. Optionally, a list of removed nodes can be added to the removedNodes parameter.

Synchronizing the contents of "Source" and "Target" nodes:

1
2
3
4
var questions = $Xml.SelectAll('Source/Questions/Question');
$Xml.Sync('Target/Questions/Question', questions, function() {
  return this.Evaluate('Id');
});

Performing an XQuery and merging the results with the current nodes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$Xml.SelectAll('Standards/Standard', function() {
  var questions = $XmlRepository.QueryXml('for $q in InternalAudit//Question\
    where $q/Standards/Standard/Standard=$standard and $q/Departments/Department=$department\
    return\
     <Question>\
        { $q/Id }\
        { $q/Criteria }\
        { $q/Expectation }\
     </Question>', {
     standard : this.Evaluate('Standard'),
     department : $Xml.Evaluate('Department')
  });
  this.Sync('Questions/Question', questions, function() {
    return this.Evaluate('Id');
  });
});

Synchronization is based on the input nodes and performs the following actions at the specified XPath:
- Node comparison is performed using the specified hash function, which returns a unique identifier for each node. If no hash function is specified, the node instance is used for comparison.
- Nodes that exist in the input array but not in the current node are automatically created in the current node.
- Existing nodes in both the input array and the current node are copied.
- Nodes that exist in the current node but not in the input array are removed from the current node. Optionally, a list of removed nodes can be added to the removedNodes parameter.

Synchronize "Source" and "Target" node contents

1
2
3
4
var questions = $Xml.SelectAll('Source/Questions/Question');
$Xml.Sync('Target/Questions/Question', questions, function() {
  return this.Evaluate('Id');
});

Performs xquery and merges results with current nodes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
$Xml.SelectAll('Standards/Standard', function() {
  var questions = $XmlRepository.QueryXml('for $q in InternalAudit//Question\
    where $q/Standards/Standard/Standard=$standard and $q/Departments/Department=$department\
    return\
     <Question>\
        { $q/Id }\
        { $q/Criteria }\
        { $q/Expectation }\
     </Question>', {
     standard : this.Evaluate('Standard'),
     department : $Xml.Evaluate('Department')
  });
  this.Sync('Questions/Question', questions, function() {
    return this.Evaluate('Id');
  });
});

See Also