Xsl conversion on xsd to xml link

I am new to XSL and need help with the transformation problem. I have an XML file that is described by XSD. I am using an XSL file to convert XML to HTML. I want to reference the XSD in an XML file, but when I do XML, it does not convert.

XML example:

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

XSL example:

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

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

In XML, if I use a commented root tag, Firefox and Chrome do not convert xml. If I just use plain <root> however, the conversion happens fine.

Can someone explain why XSL conversion does not happen if I reference XSD in my XML? Any help is appreciated!

+4
source share
1 answer
 <!-- <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://localhost" xsi:schemaLocation="http://localhost example.xsd"> --> 

This has nothing to do with using an XML schema. The problem is that you are specifying a default namespace .

Using XPath expressions for node names in the default namespace is XPath's biggest query.

Please find the xpath and xslt tags for the "default namespace" and you will find many good answers.

The solution for XSLT is to declare a namespace with some prefix (for example, "x") and namespace-uri, which matches the default namespace namespace in the XML document. Then, in any XPath expression, use x:name instead of name .

Thus, your XSLT code becomes :

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

and when applied to the provided XML document using an element without comment <root> :

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

required, the correct result is obtained :

 <ul> <li>Element 1</li> <li>Element 2</li> <li>Element 3</li> </ul> 
+8
source

Source: https://habr.com/ru/post/1334454/


All Articles