1

I have problems to find a good Javascript library to transform XML (in this case, XSD) to XML by XSL. I found some Javascript libraries online, but none of them is working.. I tried to transform it online (by 'http://www.freeformatter.com/xsl-transformer.html'), and that actually works! But when I use a Javascript library, it does not work unfortunately.. What am I doing wrong here?

This is code I have now:

<html>
<body>
<div id="output"></div>

<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="jquery.xslt.js"></script>
<script type="text/javascript">
    $(function() {
        $('#output').xslt({xmlUrl: 'notworking.xsd', xslUrl: 'notworking.xsl'});
    });
</script>
</body>
</html>

the 'notworking.xsd' file:

<xs:import schemaLocation="https://www.w3.org/2001/xml.xsd" namespace="http://www.w3.org/XML/1998/namespace"/>

<xs:element name="list">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension>
                <xs:sequence>
                    <xs:element minOccurs="0" name="list.start" />
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

the 'notworking.xsl' file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" indent="yes" encoding="utf-8"/>

    <xsl:template match="xs:schema">
        <xsl:variable name="name" select="fn:distinct-values(.//xs:element/@name)"/>
        <xsl:variable name="schema" select="."/>
        <xsl:element name="xsd">
            <xsl:for-each select=".//xs:element[@name]">
                <xsl:sort select="@name"/>
                <xsl:apply-templates select="."/>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

    <xsl:template match="xs:element">
        <xsl:variable name="type" select="fn:tokenize(@type,':')[last()]"/>
        <xsl:variable name="base" select="fn:tokenize(ancestor::xs:schema/xs:complexType[@name=$type]/xs:complexContent/xs:extension/@base,':')[last()]"/>
        <xsl:element name="{@name}">
            <xsl:copy-of select="@minOccurs"/>
            <xsl:variable name="sequence" select="./xs:complexType/xs:sequence/xs:element|ancestor::xs:schema/xs:complexType[@name=$type]//xs:extension/xs:sequence/xs:element"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:if test="normalize-space() ne ''">
            <xsl:value-of select="."/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
3
  • What do you mean when you say "it doesn't work"? What is the error? Can you create a code snippet or plunker showing the error? Commented Apr 4, 2017 at 14:26
  • There are xsl engines in most browsers (FF and MSIE at least) I think, no need for extra javascript or call somewhere Commented Apr 4, 2017 at 14:33
  • That is an XSLT 2.0 stylesheet while browsers are only supporting XSLT 1.0 so you will need to look into Saxon-CE or Saxon-JS if you want to run XSLT 2.0 in the browser. Commented Apr 4, 2017 at 16:27

1 Answer 1

1

Your stylesheet requires XSLT 2.0 support (for example, the distinct-values() function does not exist in XSLT 1.0).

The XSLT processors supplied by the major browser vendors only support XSLT 1.0.

Give Saxon-JS a try: it supports XSLT 3.0. See http://www.saxonica.com/saxon-js/index.xml (if you can read the documentation, then you are already running Saxon-JS successfully in your browser).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.