XML文件中的XML模式声明中断XSLT转换

我有一个XSD,XML和XSLT文件。

(简体)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.)

这是错误的结果:

<?xml version="1.0" encoding="UTF-8"?>
<project/>

这是我的xslt文件:

<?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>

还有我的xsd文件的第一行:

    <?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 name space. 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 or attribute. I used xmlns:a="SYSTEM" for that.

    请看下面。

    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>