0

I need to remove the namespace prefix from some xml but keep the namespace using XSLT.

This is exactly as described here - How to remove namespace prefix leaving namespace value (XSLT)?

This works fine when I test with XML copy editor but the application I'm working on in is using XSLTC. Unfortunately it does not work with XSLTC. Any ideas on how to get it working?

Here is the source xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:eBooking xmlns:ns0="http://test.com">
  <ns0:eventInfo Id="ID00000000000000000020" version="1">
    <ns0:Event>
      <ns0:tpAmb>1</ns0:tpAmb>
      <ns0:procEmi>1</ns0:procEmi>
    </ns0:Event>
  </ns0:eventInfo>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
      <Reference URI="#ID00000000000000000020">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>SzLt....==</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>MIIHdzCCBV.......==</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</ns0:eBooking>

Here is the xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns0="http://test.com"
                xmlns="http://test.com"  >
    <xsl:output indent="yes"/>
    <xsl:template match="ns0:*">
        <xsl:element name="{local-name()}" >
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

When using JDK XSLTC this gives:

<?xml version="1.0" encoding="UTF-8"?>
<eBooking>
  <eventInfo Id="ID00000000000000000020" version="1">
    <Event>
      <tpAmb>1</tpAmb>
      <procEmi>1</procEmi>
    </Event>
  </eventInfo>
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#" **xmlns:ns0="http://test.com"**>
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
      <Reference URI="#ID00000000000000000020">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
          <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
        <DigestValue>p42saeKIMnVR7/P/nzen8mTsq94=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>SzLt....==</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>MIIHdzCCBV.......==</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</eBooking>

It works fine if I test with xml copy editor.

3
  • 1
    So what happens with XSLTC? Which result do you get? Commented Feb 11, 2014 at 11:45
  • It's better to add a source XML, a desired XML and probably other relevant information like already tried XSLTs to your question. Commented Feb 11, 2014 at 12:44
  • Here is the source xml: Commented Feb 11, 2014 at 19:50

1 Answer 1

0

This looks like a bug in the version of XSLTC you're using there, it should use the default namespace declared in the stylesheet. You can probably fix that bit up with

    <xsl:element name="{local-name()}" namespace="{namespace-uri()}" >

As for the xmlns:ns0 that you've highlighted on the Signature element, this is actually correct behaviour because xsl:copy copies all namespace nodes when it copies an element. To get rid of this you'd need another template

<!-- higher priority than node() but lower than ns0:* -->
<xsl:template match="*" priority="-0.4">
  <xsl:element name="{name()}" namespace="{namespace-uri()}" >
    <xsl:apply-templates select="@*|node()" />
  </xsl:element>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.