xslt - Generic Xml Document manipulation -
We have xml documents that have many flagged nodes in which isProduct, isActive, isMandatory where node text is true or false Could.
It is necessary to manipulate documents and keep their structure, but change the above mentioned nodes in the oral representation as below:
& lt; IsProduct & gt; True & lt; / IsProduct & gt; === & gt; & Lt; Type & gt; The product's & lt; Type & gt; & Lt; IsProduct & gt; Wrong & lt; / IsProduct & gt; === & gt; & Lt; Type / & gt;
And for other flag nodes too.
We are seeking an extensible and scalable solution that can be configured with minimal friction after deployment.
by extensible; We mean there will be more cases; Like 2 flags that represent a situation; That is, employees and customers are used in the document to represent 4 different designated things; Therefore, 4 possible combinations should only be translated into a string such as "employee", "customer", "customer-employee" or "none".
By Scalable; We mean that it can be used to make any XML document prior to its schema and without restriction on the document size.
We understand that this XSLT can be used, can we write an XSLT accept any document and add or update the same document with additional nodes ?
Assuming it as an input:
& lt; Gizmo & gt; & Lt; IsProduct & gt; This is true & lt; / IsProduct & gt; & Lt; IsFoo & gt; False & lt; / IsFoo & gt; & Lt; IsBar & gt; This is true & lt; / IsBar & gt; & Lt; / Tools & gt;
The general approach will be:
& lt; Xsl: template match = "gizmo" & gt; & Lt; XSL: Copy & gt; & Lt; Xsl: Apply-Select Template = "*" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: template match = "* [substrings (local-name (), 1, 2) = 'is']" & gt; & Lt; Type & gt; & Lt; Xsl: if test = ". = 'True'" & gt; & Lt; Xsl: Select value = "sub-location (local-name (), 'is')" /> & Lt; / XSL: If & gt; & Lt; / Type & gt; & Lt; / XSL: Templates & gt;
Which produces:
& lt; Gizmo & gt; & Lt; Type & gt; Product & lt; / Type & gt; & Lt; Type / & gt; & Lt; Type & gt; Bar & lt; / Type & gt; & Lt; / Tools & gt;
A more generalized approach uses a (heavier) modified identity result:
& lt ;! - All other children (incll elements that do not begin with 'this' - & gt; & lt; xsl: variable name = 'other nodes')) = [@ * | node () [no (self: * ) Or manually: * [substring (local-name (), 1, 2)! = 'Is']] "/> & Lt ;! - Identify all "other" nodes - & gt; & Lt; Xsl: apply-templates = "$ other nodes" /> & Lt ;! - All "type" nodes fall into string - & gt; & Lt; Xsl: if test = "$ typeNodes" & gt; & Lt; Type & gt; & Lt; Xsl: variable name = "typeString" & gt; & Lt; Xsl: Apply- Choose Template = "$ Type Nodes" / & gt; & Lt; / Xsl: variable & gt; & Lt; Xsl: Select the value = "substring-after ($ type-string, '-')" /> & Lt; / Type & gt; & Lt; / Xsl: if & gt; Lit: / xsl: copy & gt; & Lt; / Xsl: Templates & gt; & Lt ;! - All of these "type" nodes have a string - & gt; & Lt; Xsl: template match = "* [substrings (local-name (), 1, 2) = 'is']" & gt; & Lt; Xsl: if test = ". = 'True' & gt; & lt; xsl: text & gt; - & lt; / xsl: text & gt; & lt; xsl: select value =" substring-a Fter ( Local-name (), 'is') "/> gt;: / XSL: template & gt; & lt;! - preventing the output of empty text nodes - & gt; ; & Lt; xsl: Template Match = "Text ()">
Takes any of the above the XML input and outputs the same structure, only the elements of the
name have a & lt; dash-delimited Type as string> node:
->
Comments
Post a Comment