" />
开发者

Generate dynamic xmlns

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" 
  ></Comm开发者_如何学Pythonon:MainPageBase>

Any ideas?


You can set the namespace of an element dynamically:

<param name="ns1" >http://localhost/ns1</param>
...
<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. ]


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


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜