tags:

views:

57

answers:

3

Hello,

I would like to dynamically generate xmlns attributes.

I want to generate this in XSL :

<Common:MainPageBase xmlns:Common="clr-namespace:ThisPartIsDynamic;assembly=ThisPartIsDynamic"> </Common:MainPageBase>

How can I do that in XSL?

Thanks, Alex

Update:

To be more precise, here is what I need to generate. The parts that I want to be able to change with variables are "THISPARTISDYNAMIC":

<Common:MainPageBase
  xmlns:Common="clr-namespace:THISPARTISDYNAMIC;assembly=THISPARTISDYNAMIC"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:df="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
  xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"  
  xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  
  xmlns:uc="clr-namespace:THISPARTISDYNAMIC"
  mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 
  ></Common:MainPageBase>

Any ideas?

+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:variable name="vDynamicPart1" select="'DynPArt1'"/>
 <xsl:variable name="vDynamicPart2" select="'DynPArt2'"/>

 <xsl:template match="/">

   <xsl:element name="Common:MainPageBase"
   namespace="clr-namespace:{$vDynamicPart1};assembly={$vDynamicPart2}"/>

 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used), produces the desired result.

Dimitre Novatchev
Thanks for your reply but "ThisPartIsDynamic" is not dynamic..I would like to do something like<Common:MainPageBase><xsl:attribute name="xmlns:Common">clr-namespace:<xsl:value-of select="someelement" />;assembly=<xsl:value-of select="someelement" /></xsl:attribute></Common:MainPageBase>but I know I can't do that...
alexbf
@alexbf: Sorry, I didn't notice that. I updated my solution and it solves your problem.
Dimitre Novatchev
It doesn't work. we get<MyControl xmlns="clr-namespace:MyNamespace;assembly=MyAssembly">But what we want really is <MyControl xmlns:DynamicName="clr-namespace:MyNamespace;assembly=MyAssembly">
alexbf
@alexbf: My solution is generating exactly what your question said was wanted. Now you want something else !!! Then, please, ask another question. Your current question is completely answered. If you don't know what exactly you want, then don't ask any question -- don't waste your readers' time!!!
Dimitre Novatchev
+1  A: 

You can set the namespace of an element dynamically:

<param name="ns1" >http://localhost/ns1&lt;/param&gt;
...
<xsl:element name="test" namespace="{$ns1}" >... </xsl:element>

But that doesn't output a namespace prefix -- it changes the default namespace on that element.

I don't think there is a way to output the prefixes with a dynamic namespace URI. Something like: <xyz:test xmlns:xyz="{$ns1}"> outputs exactly that literally: <xyz:test xmlns:xyz="{$ns1}">

If that is really the exact output you require, then I think you either have to modify the serializer, or just produce the output with a placeholder URI and do a text replacement on the output xml text.

[ XSLT does not process XML syntax. It processes XML trees. Parsing the input and serializing the output are outside of it's realm. ]

Steven D. Majewski
A: 

Take a look at the article Namespaces in XSLT, and at the section XSLT 1.0: Creating dynamic namespace nodes in particular.

newtover