redundent / kotlin-xml-builder

A lightweight type safe builder to build xml documents in Kotlin
Apache License 2.0
151 stars 17 forks source link

Multiple namespace declarations? #62

Closed ndw closed 4 months ago

ndw commented 4 months ago

Hello. I'm not that familiar with Kotlin or Kotlin DSLs. Apologies in advance if this is a stupid question.

Is it possible to generate an element with multiple namespace declarations? The constructor function seems to take only a single namespace and, while it appears to be legal to put namespace() calls in the body, they appear to have no effect:

        val ns_xsl = Namespace("xsl", "http://www.w3.org/1999/XSL/Transform")
        val ns_xs = Namespace("xs", "http://www.w3.org/2001/XMLSchema")
        val style = xml("stylesheet", namespace=ns_xsl) {
            namespace(ns_xsl)
            ...

Perhaps the constructor could have a namespaces parameter that takes a list of Namespaces?

redundent commented 4 months ago

Hi, Based on your code, you are adding ns_xsl twice which won't have any effect. I'm assuming the last line is meant to be namespace(ns_xs)? Adding lots of different parameter combinations to the constructor gets messy quick so I would suggest just using the namespace() method inside the element. If you need to add multiple, you could call the method multiple times or you could try listOf(ns1, ns2, ns3).forEach { namespace(it) } inside the element block.

ndw commented 4 months ago

Thanks. I'll take another look. (I'm not even sure what I was thinking in the example I posted!)