0

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">
1
  • 2
    To find 727 other people who have fallen into the same trap, search on "XSLT default namespace". Commented May 11, 2020 at 18:49

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

For the attributes in the sample you need to use the unprefixed name like @name or @value, they are not in any namespace.
@MartinHonnen, thanks for the clarification. I adjusted the verbiage of the proposed solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.