Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unwanted data in xslt 2

Status
Not open for further replies.

Naug

Technical User
Sep 24, 2004
85
RU
when I apply xslt transformation to an xml file the nodes for witch I didnt provide a template disappear however their contents doesnt. the only way around this seems to create a template for offending node but not to do anything with it. I realise my description is somewhat vague but could anyone tell me how to make it so that my new xml file contains only nodes which were transformed by xsl and nothing else?

 
yep it's java. I forgot to make my dom namespace aware like so

factory.setNamespaceAware(true);
//should be placed after DocumentBuilderFactory

and madness ensued

Thank you fellas for help and sorry I made you look for mistake where there was none
 
err last question - what if I have several nodes that I want to keep - do I list them all in root's select or what?
 
Yes. xsl:apply-templates will try and apply the appropriate template for a node. When none is specified, it goes to the default, which is to output text. When you have a template that matches the root node "/", this is a match for all nodes, because all the nodes are children of the root. So you can build up your XML from that template:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:text="[URL unfurl="true"]http://openoffice.org/2000/text">[/URL]
  <xsl:output method="xml"/>
  <xsl:namespace-alias stylesheet-prefix="test" result-prefix="#default"/>
  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="//text:section"/>
      <xsl:apply-templates select="mypath/somenode"/>
    </root>
  </xsl:template>
  <xsl:template match="text:section">
    <comment name="{@text:name}">
      <xsl:apply-templates/>
    </comment>
  </xsl:template>
  <xsl:template match="somenode">
    <somenode>
      <xsl:value-of select="."/>
      <xsl:apply-templates select="mypath/anothernode"/>
    </somenode>
  </xsl:template>
  <xsl:template match="anothernode">
    <anothernode>
      <xsl:value-of select="."/>
    </anothernode>
  </xsl:template>
</xsl:stylesheet>
This is how you could build up a new structure.

Hope this helps,

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top