Answered by:
xslt does not work when xml references a schema

Question
-
User-879727125 posted
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:
xmlns="http://tempuri.org/MySchema.xsd"
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:
- Fix the XSLT so it works with the XSD reference in the XML
- Modify the XmlSerializer so that the XSD is specified in some other way
- Just copy the XML file every time it has been edited and then remove the XSD reference
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.
Saturday, October 20, 2018 10:48 PM
Answers
-
User-893317190 posted
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.
<?xml version="1.0"?> <people xmlns="http://tempuri.org/MySchema.xsd"> <person born="1912" died="1954"> <name> <first_name>Alan</first_name> <last_name>Turing</last_name> </name> <profession>computer scientist</profession> <profession>mathematician</profession> <profession>cryptographer</profession> </person> <person born="1918" died="1988"> <name> <first_name>Richard</first_name> <middle_initial>M</middle_initial> <last_name>Feynman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people>
My xslt should also add namespace as follows.
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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--> <html> <head><title>Famous Scientists</title></head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="pe:name"> <p><xsl:value-of select="pe:last_name"/>, <xsl:value-of select="pe:first_name"/></p> </xsl:template> <xsl:template match="pe:person"> <xsl:apply-templates select="pe:name"/> </xsl:template> </xsl:stylesheet>
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 22, 2018 7:00 AM
All replies
-
User-893317190 posted
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.
<?xml version="1.0"?> <people xmlns="http://tempuri.org/MySchema.xsd"> <person born="1912" died="1954"> <name> <first_name>Alan</first_name> <last_name>Turing</last_name> </name> <profession>computer scientist</profession> <profession>mathematician</profession> <profession>cryptographer</profession> </person> <person born="1918" died="1988"> <name> <first_name>Richard</first_name> <middle_initial>M</middle_initial> <last_name>Feynman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people>
My xslt should also add namespace as follows.
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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--> <html> <head><title>Famous Scientists</title></head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="pe:name"> <p><xsl:value-of select="pe:last_name"/>, <xsl:value-of select="pe:first_name"/></p> </xsl:template> <xsl:template match="pe:person"> <xsl:apply-templates select="pe:name"/> </xsl:template> </xsl:stylesheet>
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 22, 2018 7:00 AM -
User-879727125 posted
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.
Monday, October 22, 2018 1:42 PM -
User-893317190 posted
Hi Sam Hobbs,
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.
Best regards,
Ackerly Xu
Tuesday, October 23, 2018 1:07 AM