Reputation: 27781
Say I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Project xmlns="http://My/Project.xsd">
<Thing Name="test"/>
</Project>
And my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
The output is [NewLine][Tab][NewLine]
which matches the spacing of the XML file.
If I change my XSLT to be: (added a prefix)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:N="http://My/Project.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="N:Thing">
<xsl:value-of select="@Name"/>
</xsl:template>
</xsl:stylesheet>
The output is [NewLine][Tab]test[NewLine]
which again matches the spacing of the XML file but includes the value of the "Name" attribute.
My expected output is simply test
. No new lines, no tabs - it should not follow the format of the XML file at all.
I want to write the XML and XSLT without using prefixes. How can I make this output what I'm expecting?
Upvotes: 0
Views: 851
Reputation: 2941
There are two issues here - first is that you don't want to specify the namespace prefix and second is that you don't want to have spaces from source document to affect your output. Let's discuss them separately.
Using namespace prefix: The short answer is no - you can not write XSL template that matches elements within particular namespace without specifying such namespace using prefix. In your first XSLT you could read template definition like "I want to select node named Thing which doesn't have any namespace" while what you really want to say is "I want to select node named Thing which has namespace http://My/Project.xsd". This is the way XPath 1.0 specification works (more details in this article).
Getting rid of spacing:
Use <xsl:strip-space elements="*"/>
instruction at the beginning of stylesheet to specify that you don't want spaces from all source elements to be preserved in output document. If you want to preserve some of them use <xsl:preserve-spaces elements="myNode">
as well.
Upvotes: 3