Hi Phillip

From: "Phillip Rhodes" <[EMAIL PROTECTED]>
> I am having a problem creating a xsl stylesheet with dom4j.  My problem is
> with namespaces.
>
> Here is what I want to create:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
> ...
> </xsl:stylesheet>
>
> Currently when I try to create the above in dom4j, I get the below
> example.  I also included the code that I used to generate the bad xsl
> stylesheet.  I would appreciate being corrected!
> Thanks.
>
> What is currently generated:
> <?xml version="1.0" encoding="UTF-8"?>
>
> <stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
> </stylesheet>
>
>          Namespace ns = new Namespace("xsl",
> "http://www.w3.org/1999/XSL/Transform";);
>          Document document = DocumentHelper.createDocument();
>          Element element = document.addElement( "stylesheet" );
>          element.add(ns);
>          element.setAttributeValue("version", "1.0");

The above adds a root element called "stylesheet" with no namespace, then
adds an "xmlns:xsl" namespace declaration to it which I don't think is what
you want. Try this instead...

    QName styleSheetQName = QName.get( "xsl:stylesheet",
"http://www.w3.org/1999/XSL/Transform"; );
    Document document = DocumentHelper.createDocument();
    Element element = document.addElement( styleSheetQName );
    element.setAttributeValue("version", "1.0");

The QName is a flyweight (or 'literal') representing the local name, the
prefix and the namespace URI. QName objects can be used like String literals
are often used in Java. e.g. I tend to do things like this...

public Foo {

    protected static final String VERSION = "version";

    protected static final QName STYLESHEET = QName.get(
        "xsl:stylesheet",
        "http://www.w3.org/1999/XSL/Transform";
    );


    public Document createStylesheet() {
        Document document = DocumentHelper.createDocument();
        Element element = document.addElement( STYLESHEET );
        element.setAttributeValue( VERSION, "1.0" );

        ...
        return document;
    }

James



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to