Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在xml中引用xsd时xsl转换问题_Xml_Xslt_Xsd - Fatal编程技术网

在xml中引用xsd时xsl转换问题

在xml中引用xsd时xsl转换问题,xml,xslt,xsd,Xml,Xslt,Xsd,我对XSL相当陌生,在转换问题上需要帮助。我有一个由XSD描述的XML文件。我使用XSL文件将XML转换为HTML。我想在XML文件中引用XSD,但当我这样做时,XML不会被转换 XML示例: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="example.xsl"?> <root> <!-- <root xmlns:xsi="http:/

我对XSL相当陌生,在转换问题上需要帮助。我有一个由XSD描述的XML文件。我使用XSL文件将XML转换为HTML。我想在XML文件中引用XSD,但当我这样做时,XML不会被转换

XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>

<root>
<!--
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost" xsi:schemaLocation="http://localhost example.xsd">
-->
  <element>Element 1</element>
  <element>Element 2</element>
  <element>Element 3</element>
</root>

要素1
要素2
要素3
示例XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <ul>
      <xsl:for-each select="root/element">
        <li><xsl:value-of select="."/></li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>

示例XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://localhost"
  xmlns="http://localhost"
  elementFormDefault="qualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="element" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

在XML中,如果我使用注释掉的根标记,Firefox和Chrome不会转换XML。但是,如果我只使用普通标记,转换会很好地进行

如果我在XML中引用XSD,有人能解释为什么XSL转换不会发生吗?感谢您的帮助

<!-- <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost" xsi:schemaLocation="http://localhost example.xsd"> -->
并且当使用未注释的
元素应用于提供的XML文档时:

<root xmlns="http://localhost"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://localhost example.xsd"> 
    <element>Element 1</element>
    <element>Element 2</element>
    <element>Element 3</element>
</root>

要素1
要素2
要素3
生成所需的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://localhost" exclude-result-prefixes="x" >

    <xsl:template match="/">
        <ul>
            <xsl:for-each select="x:root/x:element">
                <li>
                    <xsl:value-of select="."/>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>
</xsl:stylesheet>
<ul>
    <li>Element 1</li>
    <li>Element 2</li>
    <li>Element 3</li>
</ul>
  • 要素1
  • 要素2
  • 要素3

问得好,+1。请参阅我的答案以了解解释和完整的解决方案。:)除了@Dimitre correct answer之外,请注意,可以为空(或空)命名空间下的元素定义模式。谢谢您提供详细而有用的答案!一旦我有足够的声誉,我会给它一个+1