Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 5th, 2006, 05:06 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default REMOVING ATTRIBUTES WITH JS

Hi,

I am trying to remove the namespace attribute with javascript but it is not working. For some reason it seems that javascript cannot remove this type of attribute. I thought it was as simple as removing any other attribute and tried the code below but to no avail. I would appreciate very much if someone could help me with this and show me how to remove the namespace.

Cheers

tmpXml is my XmlDoc. Please see XML below.

if(tmpXml.documentElement.getAttribute("xmlns")!=n ull)
{
   tmpXml.documentElement.removeAttribute("xmlns")
}

I also trid this:

if(tmpXml.documentElement.namespaceURI != null)
{
   tmpXml.documentElement.removeAttribute("xmlns")
}

This is the xml

<AnalysisQuery xmlns="http://www.kmrsoftware.net/netquest/analysisquery.xsd">
<Analysis name="NetQuestAdex"/>
  <Measures>
    <Measure name="Total Pages"/>
    <Measure name="Cost"/>
  </Measures>
  <Rows>
    <RowDim name="Journal" level="Journal Group Name"/>
  </Rows>
  <Cols>
    <ColDim name="Time" level="Year"/>
  </Cols>
  <Filter>
    <FilterDim name="ProductID" level="Product Id">
      <Member name="PRD0099"/>
      <Member name="PRD0215"/>
    </FilterDim>
    <FilterDim type="range" name="MonthFilter" level="Year" from="2003.1" to="2004.12"/>
  </Filter>
</AnalysisQuery>
 
Old July 5th, 2006, 11:24 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You are trying to convert elements that are in a namespace, http://www.kmrsoftware.net/netquest/analysisquery.xsd, to ones that are in no namespace. Personally I'd use XSLT to do this, the following transform should do this:
Code:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()">
    <xsl:choose>
      <xsl:when test="name() != ''">
        <xsl:element name="{name()}">
          <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:copy />
  </xsl:template>
</xsl:stylesheet>
If you want to stick with DOM manipulation then you will need to loop through all the nodes and create a new element each time you find an element based on the local-name() of the old one.

Why do you want to do this anyway? Normally when people ask this it's because they have difficulty retrieving namespaced nodes, perhaps we can help solve that problem instead?

--

Joe (Microsoft MVP - XML)
 
Old July 5th, 2006, 04:20 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Many many thanks to your reply. The idea of using an xslt is quite a good one.

However, would it be possible for you to help me with your second suggestion:

"you will need to loop through all the nodes and create a new element each time you find an element based on the local-name() of the old one"

I am a bit confused about the local-name() method. Would that work for both IE and Firefox ??

This is what I have tried so far but it is not working:

tempNode = new XmlDoc()
nd = tempNode.createElement("AnalysisQuery")
tempNode.appendChild(nd)
for(i=0;i<tmpXml.documentElement.childNodes;i++)
{
  tempNode.documentElement.appendChild(tmpXml[i])

}

Another idea I had was to convert the xml to a string and use a regular expression to do that. But I am not sure how to write the regular expression to do that. Have you got any ideas of how to achieve that?

This bit would have to be removed by the regular expression: xmlns="http://www.kmrsoftware.net/netquest/analysisquery.xsd"

<AnalysisQuery xmlns="http://www.kmrsoftware.net/netquest/analysisquery.xsd">

becomes this:

<AnalysisQuery>

Cheers,

CP
 
Old July 7th, 2006, 08:03 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi,

I am still struggling to find the way to remove the namespace with the DOM. Is it possible for someone to give some pointers. This is what I have tried before:

tempNode = new XmlDoc()
nd = tempNode.createElement("AnalysisQuery")
tempNode.appendChild(nd)
for(i=0;i<tmpXml.documentElement.childNodes;i++)
{
  tempNode.documentElement.appendChild(tmpXml[i])

}

I create a new XmlDoc and create the documentElement ("AnalysisQuery") and append it to the XmlDoc. Then I loop through my tempXml doc that contains the xml I showed you and for each child noded a try to append it to the tempNode document.

But this is not working.

Cheers





Similar Threads
Thread Thread Starter Forum Replies Last Post
To include a js file into another js file jdang67 Javascript 4 February 28th, 2008 03:32 AM
Removing Comments in one go Pankaj C XSLT 3 September 9th, 2007 07:05 AM
Removing Read Only anukagni Access 5 June 13th, 2006 01:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.