XSLT: Dynamic node name insertion based on template parameter

I am trying to generate the following structure in an XSLT template.

<ns:e1> <child1>some value<child1> <child2>some value<child2> <child3>some value<child3> </ns:e1>


<ns:e2> <child1>some value<child1> <child2>some value<child2> <child3>some value<child3> </ns:e2>

or other elements ns:e3 etc (although finite), based on a template parameter (say type). Typically I could use an xls:choose construct. In such a case, I would be duplicating the child elements (whose values are also template parameters).

Is there a way in XSLT to dynamically assume the element name ns:e1 or ns:e2 so that I can put the child elements once in its parent. I could save maintenance effort later if I have change the child elements or values (change once in one place and avoid bugs due to human errors).

Thanks for your help in advance.

-------------Problems Reply------------

Yes, you may use the xsl:element instruction to do that.

Assuming you always want to have <child1>some value<child1><child2>some value<child2><child3>some value<child3> as children for your parent element, you could rewrite your code like so:

<xsl:variable name="elementName">
<!-- compute the element name here ... -->

<!-- Here we create an element having the name computed in variable elementName -->
<xsl:element name="{$elementName}" namespace="http://www.anamespace.com/and/so/on">
<child1>some value<child1>
<child2>some value<child2>
<child3>some value<child3>

Category:xslt 1.0 Views:1 Time:2018-07-10
Tags: xslt 1.0

Related post

  • XSLT dynamic node name in loop 2011-09-08

    Does anyone know if it's possible to loop through a template and pull out node values based on an iterating number. So for example, I have the following XML strucutre: <nodes> <node> <label1>Label a</label1> <value1>Valu

  • Emulating Dynamic Dispatch in C++ based on Template Parameters 2010-03-12

    This is heavily simplified for the sake of the question. Say I have a hierarchy: struct Base { virtual int precision() const = 0; }; template<int Precision> struct Derived : public Base { typedef Traits<Precision>::Type Type; Derived(Type

  • Name lookup for names not dependent on template parameter in VC++2008 Express. Is it a bug? 2010-05-05

    While experimenting a bit with C++ templates I managed to produce this simple code, for which the output is different, than I expected according to my understanding of C++ rules. void bar(double d) { std::cout << "bar(double) function called"

  • More elegant way to make a C++ member function change different member variables based on template parameter? 2010-06-01

    Today, I wrote some code that needed to add elements to different container variables depending on the type of a template parameter. I solved it by writing a friend helper class specialized on its own template parameter which had a member variable of

  • Enum argument based on template parameter? 2011-08-27

    Is it possible to generate arguments based on int type of a template ? I would like to generate something like template<int num> void func(int g,...){} if num=10 then I would like my function becomes void func(int g, int gg, ....., int gggggggg

  • Dynamic Node Names in MEL Expressions 2011-03-02

    You know how if you change a nodeName in an expression it will also change the name inside of it as well? Well, how do we go about doing this for stuff like a pointPosition, where the nodeName is inside of a string? For example: vector $v = `pointPos

  • Dynamic data type choosing based on template 2011-04-17

    suppose I have an object like this: class Spline { public: Spline(std::size_t const dim); // Quite a few functions here. One of those: vec operator()(double const t) const; // Returns vector of dimension d } Now, at most uses of this class, the dimen

  • Cannot refer to a template name nested in a template parameter 2010-05-18

    I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int> D; } Where I'm basically trying to use a template class member 'Data' of some 'Provider' class, applied to 'int', but I get the follow

  • How to use enable_if to enable member functions based on template parameter of class 2010-11-11

    In code: template<class T> struct is_builtin { enum {value = 0}; }; template<> struct is_builtin<char> { enum {value = 1}; }; template<> struct is_builtin<int> { enum {value = 1}; }; template<> struct is_builtin

  • Enabling methods in template class based on template type 2012-02-19

    I'm writing an template integer wrapper class where I'd like to provide an assigment operator based on template parameter type of the class: template<typename IntType> class secure_int { public: // enable only if boost::is_signed<IntType>

  • How can I use a colon : in a nokogiri node name? 2012-02-19

    I would like the node name in the following code to be "node:name" but instead the name is put into the text of the field. require 'nokogiri' file = File.new("/Users/adamar/code/xmler/test.xml", "w+") builder = Nokogiri::XML::Builder.new do |xml| xml

  • How change data member's type by making template parameter? 2012-03-26

    Given the following class class Foo { struct Bar { ... }; ... std::deque<Bar> m_bar; }; I want to reuse most of it and add new function for which I need to change the type of m_bar from std::deque to my version of deque. I think to make Foo<

  • xslt generate children based on split and parent node name 2011-02-03

    is it possible to do the following in xsl. I'm tring to split the contents of an element and create sub-elements based on the split. To make things trickier there are the occasional exception (ie node-4 doesn't get split). I'm wondering if there is a

  • is it possible to nest dynamic node-sets in xslt? 2011-04-14

    I'd like to know if it is possible to nest dynamic node sets in XSLT and if so, how to select them using xPath. This is part of a bigger task. I'm only showing the part that I'm stuck on. This is my XSLT: <xsl:variable name="Tables"> <xsl:fo

  • In XSLT, how can I insert matching tags that apply to different templates 2011-05-20

    I'm very new to XSLT and just haven't been able to find a solution yet to my problem. I have an xml file that looks like (and I can't change the way this xml looks, realizing it's a bit odd): <account> <name>accountA</name> </acc

  • Converting XML Node Names to Uppercase (XSLT 1.0) 2011-06-29

    I have many XSLT stylesheet(s) that transform some XML into a csv file. The XML files used are generated based on database column names which used to be converted automatically to UPPERCASE when the XML was being built - which can no longer be done (

  • xslt strip node tree based on attribute 2011-08-28

    I would like to use xslt to transform an xml file into an almost identical xml file, but strip out nodes based on an attribute. If a node has an attribute, its children and it are not copied to the output file. For instance I want to strip nodes from

  • Get first child node in XSLT using local-name() 2011-11-08

    Assume we have this simple xml ... <books> <book> <author/> <title/> </book> <book> <author/> <title/> </book> </books> I'm using this xpath to get the elements of the first book instance. /

  • XSLT multiple nodes same name same level 2010-07-28

    I have the following XML document: <ReportParameters SP="prRptActivityDetail"> <Parameter> <Name>Period Start Date</Name> <Type>Date</Type> <Control>DateTextbox</Control> <ControlName>dtePeriodSta

  • checking if the name of a node is Fox within a template P 2011-06-24

    In a template name P, i want to check if the name of the current node is Fox. I'm wondering what's the usual way that people do to do this kind of checks: <xsl:template name="P"> <xsl:if test="self::Fox"> </xsl:if> </xsl:template

  • Node Name in Chef Template? 2011-10-16

    I've been trying to figure out how to get the node's name (knife bootstrap -N) into a template. I've tried a dozen different ideas and haven't found anything that works yet. Does anyone know how to access this from a Chef recipe? Also -- is there a w

  • INSERT with dynamic table name in trigger function 2011-10-27

    I'm not sure how to achieve something like the following: CREATE OR REPLACE FUNCTION fnJobQueueBEFORE() RETURNS trigger AS $$ DECLARE shadowname varchar := TG_TABLE_NAME || 'shadow'; BEGIN INSERT INTO shadowname VALUES(OLD.*); RETURN OLD; END; $$ LAN

  • managing suggested path and file name for new excel workbook files based on templates using vba 2013-02-13

    I am trying to accomplish two things related to saving new excel files based on templates (macro-enabled) (note: I’m using an add-in): 1) At a minimum I would like to prevent the user from saving a new file based on a template back to the template fo

  • Help with multi-pass XSLT using node-set() 2009-09-28

    I need to display certain data in a tabular form, and would prefer to use multi-pass xslt using node-set() so that I can avoid deploying additional tools (like xsltproc). Right now, I'm able to perform the required task in two steps i.e. Step 1: conv

  • XSLT Adding node in the correct place 2012-04-05

    I have the following XML structure: <Main> <Node1>Definite</Node1> <Node2>Definite</Node2> <Node3>Definite</Node3> <Node4>Definite</Node4> <Node5>Definite</Node5> <Node6>Definite

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.089 (s). 11 q(s)