Last post Oct 23, 2018 01:07 AM by Ackerly Xu
Oct 20, 2018 10:48 PM|Sam Hobbs|LINK
I am using a XmlDataSource for a GridView so I need to use a xslt to transform the tags to attributes. Separate from that in WPF I am using XmlSerializer to serialize the data in and out so it can be edited. So my root element in my XML file has:
If I run the transform with that in the XML file then the output of the transform is just the root element. If I do the same thing except without the preceding xsd reference then the transform works. So I have the following possible solutions:
I am sure there is an easy answer to this but I can't find it. I hope I have provided sufficient information and this can be easily answered.
Oct 22, 2018 07:00 AM|Ackerly Xu|LINK
Hi Sam Hobbs,
If you add namespace in your xml , you should also add namespace in your xslt.
For example, if my xml is as follows.
<person born="1912" died="1954">
<person born="1918" died="1988">
<hobby>Playing the bongoes</hobby>
My xslt should also add namespace as follows.
xmlns:pe="http://tempuri.org/MySchema.xsd"> <!--define a namespace which is the same as the namespace in your xml-->
<xsl:template match="pe:people"> <!--add the namespace's prefix in your selected node-->
Oct 22, 2018 01:42 PM|Sam Hobbs|LINK
Thank you, I managed to get it to work after your explanation.
It makes sense that we need to provide the same namespace but the confusing thing is that there is not a prefix in the XML file but we need to provide a prefix in the XSLT file.
XML seems simple but things like this seem unnecessarily complex. I was frustrated and converted my data and programs to use JSON. It was more work than I expected but I got it working.
Oct 23, 2018 01:07 AM|Ackerly Xu|LINK
Prefix is only used to declare a namespace.
Because your xml has a namespace xmlns="http://tempuri.org/MySchema.xsd", no matter whether it has prefix, if you want to match the element in the xml, you should attach the namespace . This is why you should add the prefix.