xml - XSLT: insert elements if they are not present -


I have to update a lot of XML files with a simple configuration. The problem I have in my XML files - the config element is optional and they may have some configuration.

So what do I do:

    if it is missing
  1. insert element in the element element in the element from and element if they were already there - then leave it .

First :

  & lt; Root & gt; & Lt; Config & gt; & Lt ;! - This option is not exactly defined - & gt; & Lt; Element2 attr = "c" /> & Lt; / Config & gt; & Lt; / Root & gt;  

What do I do :

  & lt; Root & gt; & Lt; Config & gt; & Lt; Element1 attr = "a" /> & Lt; Element2 attr = "b" / & gt; & Lt ;! - This is not an override, but insert if missing - & gt; & Lt; Element3 attr = "c" /> & Lt; / Config & gt; & Lt; / Root & gt;  

So there are many templates in my thoughts and if it is not so, apply the first step and separate the second step within mode . But it did not work.

UPD is using XSLT 1.0, but I'm guessing switching to 2.0, there will be no problem.

The predefined elements are:

  & lt; Element1 attr = "a" /> & Lt; Element2 attr = "b" / & gt; & Lt; Element3 attr = "c" />  

IMHO, you should look from the opposite direction; Press any existing config and install your own, using the original values, using the original config or - if none found - your default value

This is an XSLT 1.0 implementation:

  & lt; Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/xsl/transform" xmlns: exsl = "http://exslt.org/common" extension-element-prefixes = "Exsl" & gt; & Lt; Xsl: output method = "xml" version = "1.0" encoding = "UTF-8" indent = "yes" /> & Lt; Xsl: strip-space element = "*" /> & Lt; Xsl: keyname = "cfg" match = "config / *" use = "local-name ()" /> & Lt; Xsl: variable name = "default-cfg" & gt; & Lt; Element1 attr = "a" /> & Lt; Element2 attr = "b" / & gt; & Lt; Element3 attr = "c" /> & Lt; / XSL: variable & gt; & Lt; Xsl: variable name = "root" = "/" /> & Lt ;! - Converting Identity - & 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 = "root" & gt; & Lt; XSL: Copy & gt; & Lt; Config & gt; & Lt; Xsl: each selection = "exsl: node-set ($ default-cfg) / *" & gt; & Lt; Xsl: call-template name = "cfg-element" /> & Lt; / XSL: for-each & gt; & Lt; / Config & gt; & Lt; Xsl: apply-select template = "node ()" /> & Lt; / XSL: Copy & gt; & Lt; / XSL: Templates & gt; & Lt; Xsl: template match = "config" /> & Lt; Xsl: Template Name = "CFG-Element" & gt; & Lt; Xsl: select variable name = "name" = "name ()" /> & Lt; Xsl: variable name = "default-value" == "@ etter" /> & Lt; Xsl: each selection = "$ root" & gt; & Lt; Xsl: variable name = "current element" = "key" ('cgg', $ name) "/> & Lt; Xsl: element name = "{$ name}" & gt; & Lt; Xsl: attribute name = "attr" & gt; & Lt; XSL: Select & gt; & Lt; Xsl: when test = "$ current element" & gt; & Lt; Xsl: Select value = "$ current element / @ etter" /> & Lt; / XSL: When & gt; & Lt; XSL: otherwise & gt; & Lt; Xsl: Select Value = "$ default-value" /> & Lt; / XSL: otherwise & gt; & Lt; / XSL: Select & gt; & Lt; / XSL: Specialty & gt; & Lt; / XSL: element & gt; & Lt; / XSL: for-each & gt; & Lt; / XSL: Templates & gt; & Lt; / XSL: stylesheet & gt;  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -