Home > Back-end >  XSLT: Set xmlns uri dynamically
XSLT: Set xmlns uri dynamically

Time:10-14

I am new to xslt, and I need to be able to inject the uri dynamically into an xmlns. I have to use version 1.0. looking around I found https://www.oxygenxml.com/archives/xsl-list/200701/msg00185.html which seems to show how to do what I want to do, but, I was unable to figure out how to do it

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://w3.org/1999/XSL/Transform"
                >
   <xsl:output method="xml" encoding="utf-8" indent="yes" />
   <xsl:param name="someparam">
   <xsl:param uri=dynamic uri>
   <xsl:template name="envelope" match="/">
      <soapenv:Envelope xmlns:soapenv="http://sxhemas.xmlsoap.org/soap/envelope/"
                     xmlns:some="http://someurl"
                     xmlns:dynamic="dynamically injected uri"
                     xmlns:dynamic2="dynamically injected uri">
          <soapenv:Header>
              <some:Data>
              </Data>
          </soapenv:Header>
          <soapenv:Body>
              <dynamic:methodCall>
                  <dynamic2:Param><xsl:value-of select="$someparam"/></dynamic2:Param>
              </dynamic:methodCall>
          </soapenv:Body>
      </soapenv:Envelope>
 </xsl:template>
</xsl:stylesheet>

i want to some how set the uri for dynamic and dynamic2

CodePudding user response:

Consider the following simplified example:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="dyn-uri">example.com/123</xsl:param>

<xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="http://sxhemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
            <xsl:element name="dynamic:methodCall" namespace="{$dyn-uri}">
                <xsl:value-of select="123"/>
            </xsl:element>
        </soapenv:Body>
    </soapenv:Envelope>
</xsl:template>

</xsl:stylesheet>

In most, if not all XSLT processors, this will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://sxhemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <dynamic:methodCall xmlns:dynamic="example.com/123">123</dynamic:methodCall>
   </soapenv:Body>
</soapenv:Envelope>

This result is semantically identical to:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://sxhemas.xmlsoap.org/soap/envelope/" xmlns:dynamic="example.com/123">
  <soapenv:Header/>
  <soapenv:Body>
    <dynamic:methodCall>123</dynamic:methodCall>
  </soapenv:Body>
</soapenv:Envelope>

which seems to be the result you expect. Any conforming XML parser will parse these two documents into an identical tree.

  • Related