I have an XSD, XML and XSLT file.
(simplified) XML:
<project
xmlns="SYSTEM"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="SYSTEM schema.xsd">
<property name="name1" value="value1">
<property name="name2" value="value2">
</project>
In my XSLT i need to perform a transformation for every element in <project>
using <xsl:for-each
tag.
But the transformation only works properly when i remove the xmlns, xmlns:xsi
and xsi:schemaLocation
attributes from <project>
.
(I of course tested it without these attributes and it works fine.)
This is the faulty result:
<?xml version="1.0" encoding="UTF-8"?>
<project/>
Here's my xslt file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<project>
<xsl:for-each select="project/*">
<property>
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="@value"/></xsl:attribute>
</property>
</xsl:for-each>
</project>
</xsl:template>
</xsl:stylesheet>
And the top lines of my xsd file:
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="SYSTEM" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
targetNamespace="SYSTEM"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="project">
Your XML has a default namespace. So your XSLT needs to define it with some prefix. And you need to prepend that namespace prefix when you are referring to any element. I used xmlns:a="SYSTEM"
for that.
Please see below.
XSLT
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="SYSTEM">
<xsl:template match="/">
<xsl:value-of select="a:project/a:property"/>
</xsl:template>
</xsl:stylesheet>