Hi everybody, I need to take an xml file and remove 2 certain namespaces from it using XSLT("scrubbing metadata"). Also, I believe I would need to use the Transformer class in combination with the XSLT. Can someone help me out? Possibly giving me an example with namespace "A" and namespace "B"? Thanks.

Working with namespaces can be very easy most of the time. However, the specifics of the transformation will depend on what your input document looks like in detail. Can you make a small sample input and desired output XML document and I'll work something up. Please use Codebox as well.

This is what the xml looks like:

- <work-order-list>
- <wo:job xmlns:wo="http:hello" freshness:timestamp="2010-03" xmlns:freshness="http://freshness" history:timestamp="2010" 
xmlns:history="http:history"  
xmlns:mnr="http:mnr">
- <wo:work-order id="123" status="k" freshness:timestamp="2010-03">

This is what I want it to look like:

- <work-order-list>
- <wo:job xmlns:wo="http:hello" freshness:timestamp="2010-03" xmlns:freshness="http://freshness" history:timestamp="2010" 
>
- <wo:work-order id="123" status="k" freshness:timestamp="2010-03">

This should do what you want:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:freshness="http://freshness"
  xmlns:history="http:history"
  xmlns:wo="http:hello">

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <!-- go process children (applies to root node only) -->
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <!-- go process attributes and children -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="wo:*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <!-- go process attributes and children -->
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@history:*">
    <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@freshness:*">
    <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

For the following document:

<work-order-list>
   <wo:job xmlns:wo="http:hello" freshness:timestamp="2010-03" xmlns:freshness="http://freshness" history:timestamp="201
0" xmlns:history="http:history"  xmlns:mnr="http:mnr">
       <wo:work-order id="123" status="k" freshness:timestamp="2010-03" />
   </wo:job>
</work-order-list>

here is the ouput using xsltproc as the processor:

$ xsltproc file.xsl file.xml
<?xml version="1.0"?>
<work-order-list>
   <wo:job xmlns:wo="http:hello" xmlns:freshness="http://freshness" xmlns:history="http:history" freshness:timestamp=
10-03" history:timestamp="2010">
       <wo:work-order id="123" status="k" freshness:timestamp="2010-03"/>
   </wo:job>
</work-order-list>
$

Thank you so much. Is there a possible way for you to tell me about the transformer class and how to utilize it to achieve the desired XML after I finish the xslt?

Sorry but I do not have the time to do so. Since you are calling a stylesheet a transformation class, I suspect that your knowledge of XSL is minimum. I suggest you invest in a good book on XSLT either by Jenni Tennison or Michael Kay.

Okay, thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.