Let say I have the following XML Code
<PanelWrapper id="RootPanel"
width="800"
height="600"
forecolor="Black">
<ButtonWrapper id="button"
text="click!"
forecolor="Black">
<TabIndex>0</TabIndex>
<TabStop>True</TabStop>
</ButtonWrapper>
</PanelWrapper>
This Code should be transformed into XAML Code using XSLT 1.0:
<WrapPanel Name="RootPanel"
Width="800"
Height="600">
<Button Name="button"
Content="click!"
Foreground="Black"
TabIndex="0"
IsTabStop="True">
</Button>
</WrapPanel>
I'm currently using this Style sheet: (simplified version)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<!-- Transform PanelWrapper to WrapPannel -->
<xsl:template match="PanelWrapper">
<WrapPanel>
<xsl:apply-templates select="@*|*" />
</WrapPanel>
</xsl:template>
<!-- Transform ButtonWrapper to Button -->
<xsl:template match="ButtonWrapper">
<Button>
<xsl:apply-templates select="@*" />
</Button>
</xsl:template>
<!-- some other mapping for id(name), height, weight, text -->
<!-- Map forecolor to Foreground -->
<xsl:template match="@forecolor">
<xsl:attribute name="Foreground">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
The result I'm currently getting looks like this:
<WrapPanel Name="RootPanel"
Width="800"
Height="600"
Foreground="Black"> //unwanted, Problem 1
<Button Name="button"
Content="click!"
Foreground="Black">
</Button>
0 //Problem 2
True //Problem 2
</WrapPanel>
Problem 1: The WrapPanel in XAML has no Foreground Property but since the there is an forecolor defined in my XML source file it is generated for the WrapPanel too. How can I solve this?
Problem 2: How can I fix the issue with the TabIndex and IsTabStop?
EDIT: Let me clarify problem 2. this is the xml input:
<ButtonWrapper id="button"
text="click!"
forecolor="Black">
<TabIndex>0</TabIndex>
<TabStop>True</TabStop>
</ButtonWrapper>
and this is the output i try to achieve:
<Button Name="button"
Content="click!"
Foreground="Black"
TabIndex="0"
IsTabStop="True">
</Button>
Issue 1: BorderThickness is still displayed for WrapPanels [Solved]
Solution: The template is only called for all elements but the PanelWrapper. (see if condition)
Issue 2: Since the groupbox in WPF accepts only one control I had to wrap all properties into an grid control first. I thought it would be no big deal but it turns out that the groupbox attributes are added to the grid control.
Here comes the (updated) xsl Stylesheet:
Here comes the xml file which should be converted into xaml code
The XAML result looks like this:
The expected XAML code should look like this:
btw: Pablo Pozo, I owe you at least two beer
Problem 1. You could add the following template
which is going to exclude the attributes named forecolor for all the PanelWrapper elements.
Problem 2. To solve your second problem you have to match all TabIndex elements and TabStop elements which are children of some element using the following XPath expressions respectively : */TabIndex, */TabStop. Then you can map that element to an attribute.
The following transformation achieves what you want:
UPDATE. Added solution for complete XML.