3.7 Handle XSLT

3.7.1 Outline

Associate a name to the result of applying the named XSLT transformation on the named XML code. If the name is omitted, the outcome is sent to the output file named in the command line. If such file name is not provided, the standard I/O stream is assumed.

<xslt  name="..." xml="..."  xsl="..." />

Note that XSLT is just a restricted kind of XML code.

<..xtpipes entries..>+
 | xslt
 -_-_-

<..xtpipes.dtd..>+
 <!ELEMENT xslt EMPTY >
 <!ATTLIST xslt
           name CDATA #IMPLIED
           xml  CDATA #REQUIRED
           xsl  CDATA #REQUIRED
 >
 -_-_-

<..execute xslt..>
 try{
    <.StreamSource inDoc = ....>
    <.StreamSource inXslt = ....>
    <.StreamResult outDoc = ....>
    errMssg = null;
    fc.setErrorListener( <.transformer factory XSLT error listener.> );
    Transformer transformer = fc.newTransformer( inXslt );
    transformer.setErrorListener( <.transformer XML error listener.> );
    transformer.transform(inDoc, outDoc );
    <.store xslt outcome.>
 } catch ( javax.xml.transform.TransformerException e ){
    if( Xtpipes.trace ){ e.printStackTrace(); }
    instructionErr( node,
                    e.getMessage()
                    +
                    ((errMssg==null)? "" : ("; " +errMssg))
                    , 37);
 } catch ( Exception e ){
    if( Xtpipes.trace ){ e.printStackTrace(); }
    instructionErr( node, (errMssg==null)? e.toString()
                                         : e.toString() + "; " + errMssg
                        , 16 );
 }
 -_-_-

3.7.2 Input

<..StreamSource inDoc = .....>
 Node xmlNode = node.getAttributes().getNamedItem( "xml" );
 StreamSource inDoc = null;
 String xml;
 if( xmlNode == null ){
    <.inDoc = input stream source.>
 } else {
    xml = xmlNode.getNodeValue();
    String doc = (String) map.get(xml);
    if( doc!=null ){
       byte [] bytes = doc.getBytes("UTF-8");
       ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
       inDoc = new StreamSource( bais );
 }  }
 -_-_-

<..inDoc = input stream source..>
 if( inData == null ){
    inDoc = new StreamSource( new File(inFile) );
 } else {
    byte [] bytes = inData.getBytes("UTF-8");
    ByteArrayInputStream bais = new ByteArrayInputStream( bytes );
    inDoc = new StreamSource( bais );
 }
 -_-_-

3.7.3 XSLT

<..StreamSource inXslt = .....>
 String xslt = node.getAttributes()
                   .getNamedItem( "xsl" ).getNodeValue();
 String templates = (String) map.get(xslt);
 byte [] bytes = templates.getBytes("UTF-8");
 ByteArrayInputStream bais =  new ByteArrayInputStream( bytes );
 StreamSource inXslt = new StreamSource( bais );
 -_-_-

3.7.4 Output

<..StreamResult outDoc = .....>
 Node nameNode = node.getAttributes().getNamedItem("name");
 StreamResult outDoc;
 CharArrayWriter caos = null;
 if( nameNode == null ){
    outDoc = new StreamResult(outPrintWriter);
    returnToFile = false;
 } else {
    caos = new CharArrayWriter();
    outDoc  = new StreamResult(caos);
 }
 -_-_-

<..store xslt outcome..>
 <.store chars into map.>
 -_-_-

<..NO..>
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 StreamResult outDoc  = new StreamResult(baos);
 -_-_-

<..NO..>+
 String name = node.getAttributes()
                 .getNamedItem( "name" ).getNodeValue();
 bytes = baos.toByteArray();
 map.put( name, (Object) new String(bytes) );
 -_-_-