0
votes

I am trying to validate a Yang model against an XML instance. I found this bash script[1] that does the job, but I want to rewrite it using Java.

Saxon version

Saxon 9.9.1-5.

xsltproc version

Using libxml 20904, libxslt 10129 and libexslt 817

Using xsltproc from Bash, the following commands succeed and result in a valid xml, sch2, that contains the exslt namespace.

xsltproc -o output1 iso_abstract_expand.xsl example.sch
xsltproc -o sch iso_svrl_for_xslt1.xsl output1
xsltproc -o sch2 sch inst

When I rewrite the above commands in Java i get

Static error at xsl:stylesheet on line 17 column 31 
XTSE1430: Namespace prefix exsl is undeclared

Java Code

class Test {
    public static StringBuffer transform(Source xsltSource, Source xmlSource) throws TransformerException {
        TransformerFactory transFact = new TransformerFactoryImpl();
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        Transformer transformer = transFact.newTransformer(xsltSource);
        transformer.transform(xmlSource, result);
        return sw.getBuffer();
    }

    public static StringBuffer xsltproc(String xslt, String xml) throws TransformerException {
        return transform(
                new StreamSource(new File(xslt)),
                new StreamSource(new File(xml))
        );
    }

    public static StringBuffer xsltproc(String xslt, StringBuffer xml) throws IOException, TransformerException {
        return transform(
                new StreamSource(new File(xslt)),
                new StreamSource(IOUtils.toInputStream(xml))
        );
    }

    public static StringBuffer xsltproc(StringBuffer xslt, String xml) throws IOException, TransformerException {
        return transform(
                new StreamSource(IOUtils.toInputStream(xslt)),
                new StreamSource(new File(xml))
        );
    }

    public static void main(String args[]) {
        try {
            StringBuffer output1 = xsltproc("iso_abstract_expand.xsl", "example.sch");
            StringBuffer sch = xsltproc("iso_svrl_for_xslt1.xsl", output1);
            StringBuffer sch = xsltproc(sch, "inst");
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Where "iso_abstract_expand.xsl", "example.sch", "iso_svrl_for_xslt1.xsl","inst" are files on disk that reside in the same folder as the above Java class.

iso_abstract_expand.xsl

https://github.com/mbj4668/pyang/blob/master/xslt/iso_abstract_expand.xsl

iso_svrl_for_xslt1.xsl

https://github.com/mbj4668/pyang/blob/master/xslt/iso_svrl_for_xslt1.xsl

example.sch

<?xml version="1.0" encoding="utf-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="exslt"><sch:ns uri="http://exslt.org/dynamic" prefix="dyn"/><sch:ns uri="http://example.net/turing-machine" prefix="tm"/><sch:ns uri="urn:ietf:params:xml:ns:netconf:base:1.0" prefix="nc"/><sch:let name="root" value="/nc:config"/><sch:pattern abstract="true" id="turing-machine___tape-cells"><sch:rule context="$start/$pref:cell"><sch:report test="preceding-sibling::$pref:cell[$pref:coord=current()/$pref:coord]">Duplicate key "coord"</sch:report></sch:rule></sch:pattern><sch:pattern id="turing-machine"><sch:rule context="/nc:config/tm:turing-machine/tm:transition-function/tm:delta"><sch:report test="preceding-sibling::tm:delta[tm:label=current()/tm:label]">Duplicate key "tm:label"</sch:report><sch:report test="preceding-sibling::tm:delta[tm:input/tm:state=current()/tm:input/tm:state and tm:input/tm:symbol=current()/tm:input/tm:symbol]">Violated uniqueness for "tm:input/tm:state tm:input/tm:symbol"</sch:report></sch:rule></sch:pattern><sch:pattern id="idm14" is-a="turing-machine___tape-cells"><sch:param name="start" value="/nc:config/tm:turing-machine/tm:tape"/><sch:param name="pref" value="tm"/></sch:pattern></sch:schema>

inst

<?xml version="1.0" encoding="utf-8"?>
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><turing-machine xmlns="http://example.net/turing-machine"><transition-function><delta><label>left summand</label><input><state>0</state><symbol>1</symbol></input><tm:output xmlns:tm="http://example.net/turing-machine"><?dsrl?><tm:head-move>right</tm:head-move></tm:output></delta><delta><label>separator</label><input><state>0</state><symbol>0</symbol></input><output><state>1</state><symbol>1</symbol><tm:head-move xmlns:tm="http://example.net/turing-machine"><?dsrl?>right</tm:head-move></output></delta><delta><label>right summand</label><input><state>1</state><symbol>1</symbol></input><tm:output xmlns:tm="http://example.net/turing-machine"><?dsrl?><tm:head-move>right</tm:head-move></tm:output></delta><delta><label>right end</label><input><state>1</state><symbol/></input><output><state>2</state><head-move>left</head-move></output></delta><delta><label>write separator</label><input><state>2</state><symbol>1</symbol></input><output><state>3</state><symbol>0</symbol><head-move>left</head-move></output></delta><delta><label>go home</label><input><state>3</state><symbol>1</symbol></input><output><head-move>left</head-move></output></delta><delta><label>final step</label><input><state>3</state><symbol/></input><output><state>4</state><tm:head-move xmlns:tm="http://example.net/turing-machine"><?dsrl?>right</tm:head-move></output></delta></transition-function></turing-machine></config>

Indeed the output of the last Java command does not have the exslt namespace while the output of the last command in Bash(using xsltproc) contains the exslt namespace

xmlns:exsl="http://exslt.org/common"

[1] https://github.com/mbj4668/pyang/blob/master/bin/yang2dsdl

1
The file iso_svrl_for_xslt1.xsl has a comment suggesting to change an import, if I understand that correctly to comment out the import <xsl:import href="iso_schematron_skeleton_for_xslt1.xsl"/> and to uncomment the one for Saxon <xsl:import href="iso_schematron_skeleton_for_saxon.xsl"/>. I haven't checked whether that relates to Saxon 9 or Saxon 6.Martin Honnen

1 Answers

1
votes

The error XTSE1430 means that there is an extension-element-prefixes attribute containing a namespace prefix (exsl) which has not been declared.

It's not clear from the information given how the extension-element-prefixes attribute is generated, but it's not Saxon that's generating it, it's user-written XSLT code, and the responsibility for ensuring that the namespace is declared therefore falls on the user-written code.

Saxon will always ensure that namespace prefixes used in the names of elements and attributes are declared (a process called "namespace fixup") but it is not able to do this for prefixes used in the content of text or attribute nodes: that's a user responsibility.

I haven't attempted to study the source code in the files that you link to.