Reputation: 195
New to XSLT, so I'm probably doing something stupid.
I have a bunch of XML documents that I am needing to convert to WPF Resource Dictionaries. (Changing the way string resources are handled.)
The nodes are converting fine, but I am having an issue with the namespace declarations being outputted to the child elements, when they should be on the ResourceDictionary element.
Sample XML:
<root>
<data name="StringName" xml:space="preserve">
<value>String Value</value>
<comment>String Description</comment>
</data>
</root>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="Localization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:element name="ResourceDictionary" >
<xsl:apply-templates select="root/data" />
</xsl:element>
</xsl:template>
<xsl:template match="data">
<xsl:element name="loc:Text" >
<xsl:attribute name="x:Key">
<xsl:value-of select="@name" />
</xsl:attribute>
<xsl:attribute name="Content">
<xsl:value-of select="value" />
</xsl:attribute>
<xsl:attribute name="TranslComment">
<xsl:value-of select="comment" />
</xsl:attribute>
<xsl:attribute name="FormatComment">
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Current Output:
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<loc:Text xmlns:loc="Localization" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Key="StringName" Content="String Value" TranslComment="String Description" FormatComment=""/>
</ResourceDictionary>
I've tried putting the namespace declarations everywhere, using namespace attributes and xsl:attribute nodes. I get the same results each time.
What am I missing? Thanks all.
Upvotes: 1
Views: 315
Reputation: 70598
If you want all the namespace declarations on the root element, try declaring them on the root element in the XSLT
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="Localization">
<xsl:apply-templates select="root/data" />
</ResourceDictionary>
(Note, there is no need to use xsl:element
to create an element if it is a static name)
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="Localization"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:loc="Localization">
<xsl:apply-templates select="root/data" />
</ResourceDictionary>
</xsl:template>
<xsl:template match="data">
<loc:Text x:Key="{@name}" Content="{value}" TranslComment="{comment}" FormatComment="" />
</xsl:template>
</xsl:stylesheet>
Also note the use of Attribute Value Templates to create the attributes on the loc:Text
element.
Upvotes: 3