xml - XSLT to Transform the child Node name according to their occurrence -


I need to write an XSLT, which will change the node name, I will see it during my presence < / P>

XML Current: & lt; Order & gt; & Lt; Order & gt; & Lt; OrderNo & gt; ABC & lt; / OrderNo & gt; & Lt; ItemDetails & gt; & Lt; Name & gt; Shirt & lt; / Name & gt; & Lt; Name & gt; Socks & lt; / Name & gt; & Lt; ItemPrice & gt; 30 & lt; / ItemPrice & gt; & Lt; ItemPrice & gt; 40 & lt; / ItemPrice & gt; & Lt; / ItemDetails & gt; & Lt; / Order & gt; & Lt; / Order & gt;

XML Requirement

  & lt; Order & gt; & Lt; Order & gt; & Lt; OrderNo & gt; ABC & lt; / OrderNo & gt; & Lt; ItemDetails & gt; & Lt; NAME1 & gt; Shirt & lt; / Name & gt; & Lt; NAME2 & gt; Socks & lt; / Name & gt; & Lt; ItemPrice1 & gt; 30 & lt; / ItemPrice & gt; & Lt; ItemPrice2 & gt; 40 & lt; / ItemPrice & gt; & Lt; / ItemDetails & gt; & Lt; / Order & gt; & Lt; / Order & gt;   

Check the name and item price prize. In this way I have 100 commands in an XML of commands

It is possible to change the identity changes and just change the bit bits that you want to change to make the most XML unchanged copy using a stylesheet:

 < Code> & lt; Xsl: stylesheet xmlns: xsl = "http://www.w3.org/1990/XSL/transform" version = "1.0" & gt; & Lt; Xsl: template match = "@ * | node ()" & gt; & Lt; xsl: Copy & gt; & Lt; xsl: apply-select template = "@ * | node ()" /> & Lt; / xsl: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: Template Match = "Item Details / *" & gt; & Lt; Xsl: element name = "{name ()} {count (predecessor-cybling :: * [name () = name (current ())]) + 1}" & gt; & Lt; xsl: apply-select template = "@ * | node ()" /> & Lt; / XSL: element & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;   

If you want to leave the first event of each element alone and start the number from the other, then you can do the template match in XSLT 2.0 By tweaking the pattern:

  & lt; Xsl: template match = "item info / * [predecessor-cybling :: * [name () = name (current ())]]" & gt; & Lt; Xsl: element name = "{name ()} {count (predecessor-cybling :: * [name () = name (current ())])" & gt; & Lt; xsl: apply-select template = "@ * | node ()" /> & Lt; / XSL: element & gt; & Lt; / XSL: Templates & gt;   

But in XSLT 1.0 you can not use the current in match pattern, so you can use it for the template's body. Check inside, like

  & lt; xsl: template match = "@ * | node ()" name = "ident" & gt; & Lt; xsl: Copy & gt; & Lt; xsl: apply-select template = "@ * | node ()" /> & Lt; / xsl: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: Template Match = "Item Details / *" & gt; & Lt; XSL: Select & gt; & Lt; Xsl: when test = "predecessor-siblings :: * [name () = name (current ())]" & gt; & Lt; Xsl: element name = "{name ()} {count (predecessor-cybling :: * [name () = name (current ())])" & gt; & Lt; xsl: apply-select template = "@ * | node ()" /> & Lt; / XSL: element & gt; & Lt; / XSL: When & gt; & Lt; XSL: otherwise & gt; & Lt ;! - This is the first element of this name, so return it back to practice behavior - & gt; & Lt; Xsl: call-template name = "identity" /> & Lt; / XSL: otherwise & gt; & Lt; / XSL: Select & gt; & Lt; / XSL: Templates & gt;   

At the more general level, this is not a very bad option for XML format - more to use and express similar personality to use similar names in the same elements. Normal is the reliance between different elements through nesting rather than the situation, such as

     

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -