Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7744203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:42:43+00:00 2026-06-01T09:42:43+00:00

I have an XSLT stylesheet that transforms an XML file to JSON format and

  • 0

I have an XSLT stylesheet that transforms an XML file to JSON format and a part of the schema for the XML allow any XML what-so-ever, which I need to just encode as a string in the corresponding JSON.

I have the elements getting created OK I am just having trouble getting the namespaces to behave correctly.

The problem is that every single namespace in scope of the current element is being output for every single element. I only want to output namespaces as they are first declared in the original XML.

This is what I am currently trying to do, I have tried a few things and cannot figure out a way to do what I need.

I call a template that is responsible for transforming attributes for the current node with:

<xsl:call-template name="elementAttributes">
        <xsl:with-param name="element" select="."/>
</xsl:call-template>

With the following template:

<xsl:template name="elementAttributes">
        <xsl:param name="element"/>
        <xsl:for-each select="$element/namespace::*">
            <!--dont include default namespace-->
            <xsl:if test="boolean(name())">
                <xsl:text> </xsl:text>
                <xsl:value-of select="name()"/>
                <xsl:text>='</xsl:text>
                <xsl:value-of select="."/>
                <xsl:text>'</xsl:text>
            </xsl:if>
        </xsl:for-each>
</xsl:template>

So for instance with the XML:

<valueString>
        <dms:animal xmlns:dms="http://test.net/2012/dms/">
            <dms:dog>woof</dms:dog>
            <dms:cat>meow</dms:cat>
            <reptile:crocodile xmlns:reptile="http://reptile.test.net/2012/dms/"></reptile:crocodile>
        </dms:animal>
</valueString>

I am ending up with:

<dms:animal dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>
<dms:dog dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>woof</dms:dog>
<dms:cat dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' xml='http://www.w3.org/XML/1998/namespace'>meow</dms:cat>
<reptile:crocodile dcds='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' dms='http://test.net/2012/dms/' ns0='http://purl.org/dc/xmlns/2008/09/01/dc-ds-xml/' reptile='http://reptile.test.net/2012/dms/' xml='http://www.w3.org/XML/1998/namespace'/>

when I only basically want to get back the same XML.

I know it is possible to do copy-of to easily copy XML to an output but I since I am outputting the results as text due to the JSON format I could not get it to work that way, as only the text nodes would be copied and not the XML structure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:42:44+00:00Added an answer on June 1, 2026 at 9:42 am

    I would say that what you are trying to do is a pretty weird design. All my instincts are to go back to the requirements stage and look at whether a different design would make more sense.

    However, if you really want to do what you are trying to do, you will have to examine each namespace and output it only if the namespace is in scope for this element and not for the parent element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have to build an XSLT stylesheet that transforms an xml like this: <message>
I have a Java servlet which generates XML, translates it with an XSLT stylesheet,
I have written an xslt that reads some xml file names and does some
I have an XML file that I want to transform using an XSLT. It
I have an XML file that I am transforming via XSLT. I am passing
I have a simply XSLT 1.0 stylesheet, that turns XML documents in XHTML. I
I have got an XSLT that looks like this: <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=xml
Lets say I have a xslt stylesheet like the following: <?xml version=1.0 encoding=utf-8?> <xsl:stylesheet
i have the following xslt sheet: <?xml version=1.0 encoding=UTF-8 ?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version=1.0> <xsl:variable
I've generated the following XSLT file, and have created a Form that will post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.