Pass document as parameter to XSL Translation in Java

后端 未结 4 1646
无人及你
无人及你 2021-01-12 11:01

I\'m working on addition internationalization to my XSL. I\'ve seen plenty of examples of creating a dictionary.xml file and loading it into my XSL via document(\'dictionar

4条回答
  •  自闭症患者
    2021-01-12 11:54

    Use a URIResolver instead of a parameter. First, create the resolver like this:

    public class DocURIResolver implements URIResolver {
    
        final Map documents;
    
        public DocURIResolver(final Map documents) {
            this.documents = documents;
        }
    
        public Source resolve(final String href, final String base) {
            final Document doc = documents.get(href);
            return (doc != null) ? new DOMSource(doc) : null;
        }
    }
    

    Use it like this:

    Document dictionary = TranslationDictionary.getDictionaryDocument();
    Map docs = new HashMap();
    docs.put("dictionary", dictionary);
    // transformer is your javax.xml.transform.Transformer
    transformer.setURIResolver(new DocURIResolver(docs));
    

    And reference it in your stylesheet by name:

    
    

    This is just a toy example, of course. You can make your URIResolver as full-featured as necessary.

提交回复
热议问题