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?

 
Default template is used when none is specified (just output value). Therefore you need to specify a template. What you can do it specify a template for the root node and work from there.

So, at the top of your stylesheet put:
Code:
<xsl:template match="/">
  <xsl:apply-templates select="x"/>
</xsl:template>
Replace "x" with whatever node you want to specify a template for.
 
thing is there is a sh... a lot of nodes that I want ignored (essentially I just want to select one type of node out of whole document) And I may not even know what nodes except the one that interests me may be there. Isnt there some way to make a common template that would match all nodes except for specified? Smth along the lines of match=!"myNodeName"?
 
Thats what the template I gave does. At the moment you have a template that says:
Code:
<xsl:template match="mynode">
.......
Simply put what I said above it:
Code:
<xsl:template match="/">
  <xsl:apply-templates select="mynode"/>
</xsl:template>
<xsl:template match="mynode">
.......
 
If I try that then all data disappears. Here is some code.

When I try

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns... version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>
    <xsl:template match="text:section">
      <comment name="{@text:name}">
          <xsl:apply-templates/>
           </comment>
    </xsl:template>
</xsl:stylesheet>

I get everything as discribed in first post i.e. only the nodes that I need plus a lot of node-less trash

When I do as you suggested (or rather this is my understanding of what you are suggesting)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns... version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates  select="text:section"/>
        </root>
    </xsl:template>
    <xsl:template match="text:section">
      <comment name="{@text:name}">
          <xsl:apply-templates/>
           </comment>
    </xsl:template>
</xsl:stylesheet>

I only get empty root node (i.e. <root .../>)
 
u need xsl:copy-of:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns... version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates  select="text:section"/>
        </root>
    </xsl:template>
    <xsl:template match="text:section">
      <comment name="{@text:name}">
          <xsl:copy-of select="*"/>
           </comment>
    </xsl:template>
</xsl:stylesheet>

 
Its unclear what you are trying to do. If flumpy's answered the question then great, but if not, post your XML (or sample of) and the XML of what you expect the output to be.
 
nope - still empty root tag
 
Post your XML and we can give a much better answer. However, try:
Code:
<xsl:apply-templates  select="//text:section"/>
Although for MUCH faster processing specify the node explicitly:
Code:
<xsl:apply-templates  select="mypath/text:section"/>
 
My previous reply was to flumpy*
my original file is xml from OOo (I've cut out some style information since it is a bit lengthy)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE office:document-content PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "office.dtd">
<office:document-content xmlns:office="[URL unfurl="true"]http://openoffice.org/2000/office"[/URL] xmlns:style="[URL unfurl="true"]http://openoffice.org/2000/style"[/URL] xmlns:text="[URL unfurl="true"]http://openoffice.org/2000/text"[/URL] xmlns:table="[URL unfurl="true"]http://openoffice.org/2000/table"[/URL] xmlns:draw="[URL unfurl="true"]http://openoffice.org/2000/drawing"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format"[/URL] xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:number="[URL unfurl="true"]http://openoffice.org/2000/datastyle"[/URL] xmlns:svg="[URL unfurl="true"]http://www.w3.org/2000/svg"[/URL] xmlns:chart="[URL unfurl="true"]http://openoffice.org/2000/chart"[/URL] xmlns:dr3d="[URL unfurl="true"]http://openoffice.org/2000/dr3d"[/URL] xmlns:math="[URL unfurl="true"]http://www.w3.org/1998/Math/MathML"[/URL] xmlns:form="[URL unfurl="true"]http://openoffice.org/2000/form"[/URL] xmlns:script="[URL unfurl="true"]http://openoffice.org/2000/script"[/URL] office:class="text" office:version="1.0">
 <office:script/>
 <office:font-decls>
   </office:font-decls>
 <office:automatic-styles>
  </office:automatic-styles>
 <office:body>
  <text:sequence-decls>
   <text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
   <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
   <text:sequence-decl text:display-outline-level="0" text:name="Text"/>
   <text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
  </text:sequence-decls>
  <table:table table:name="My table" table:style-name="My table">
...
   <table:table-row>
    <table:table-cell table:style-name="My table.B3" table:number-columns-spanned="2" table:value-type="string">
     <text:section text:style-name="Sect1" text:name="Comm1">
      <text:p text:style-name="Standard">comment1</text:p>
     </text:section>
    </table:table-cell>
    <table:covered-table-cell/>
   </table:table-row>
  </table:table>
  <text:section text:style-name="Sect1" text:name="Comm2">
   <text:p text:style-name="Standard">comment2</text:p>
  </text:section>
 </office:body>
</office:document-content>

what I want to recieve after xsl transformation is

Code:
<root>
<comment>comment1</comment>
<comment>comment2</comment>
</root>

For this I have used the first piece of code that I posted on 16 Mar 05 6:18 but as I said earlier it leaves a lot of unwanted junk.
 
OK this seems to work

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns...>
<xsl:eek:utput method="xml"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="//text:section"/>
</root>
</xsl:template>
<xsl:template match="text:section">
<comment name="{@text:name}">
<xsl:apply-templates/>
</comment>
</xsl:template>
</xsl:stylesheet>

 
no :( I noticed another thing btw - the code that I claimed to work doesnt do so propperly - i.e. it just filters out all node elements from the document and leaves all text as-is. I have a stylesheet with similar structure which works just fine though. The only difference that I noticed is that it doesnt use namespaces. Could it be that xsl cant match tag with prefix?
 
You need to specify the prefix:
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"/>
    </root>
  </xsl:template>
  <xsl:template match="text:section">
    <comment name="{@text:name}">
      <xsl:apply-templates/>
    </comment>
  </xsl:template>
</xsl:stylesheet>
 
you need all the namespaces that are in the xml doc in the xsl stylesheet.

i performed a transform with the above and it does work fine afaics

full xsl

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet  xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:office="[URL unfurl="true"]http://openoffice.org/2000/office"[/URL] xmlns:style="[URL unfurl="true"]http://openoffice.org/2000/style"[/URL] xmlns:text="[URL unfurl="true"]http://openoffice.org/2000/text"[/URL] xmlns:table="[URL unfurl="true"]http://openoffice.org/2000/table"[/URL] xmlns:draw="[URL unfurl="true"]http://openoffice.org/2000/drawing"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format"[/URL] xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:number="[URL unfurl="true"]http://openoffice.org/2000/datastyle"[/URL] xmlns:svg="[URL unfurl="true"]http://www.w3.org/2000/svg"[/URL] xmlns:chart="[URL unfurl="true"]http://openoffice.org/2000/chart"[/URL] xmlns:dr3d="[URL unfurl="true"]http://openoffice.org/2000/dr3d"[/URL] xmlns:math="[URL unfurl="true"]http://www.w3.org/1998/Math/MathML"[/URL] xmlns:form="[URL unfurl="true"]http://openoffice.org/2000/form"[/URL] xmlns:script="[URL unfurl="true"]http://openoffice.org/2000/script"[/URL] office:class="text" office:version="1.0" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates  select="//text:section"/>
        </root>
    </xsl:template>
    <xsl:template match="text:section">
      <comment name="{@text:name}">
          <xsl:apply-templates/>
       </comment>
    </xsl:template>
</xsl:stylesheet>

full xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="[URL unfurl="true"]http://openoffice.org/2000/office"[/URL] xmlns:style="[URL unfurl="true"]http://openoffice.org/2000/style"[/URL] xmlns:text="[URL unfurl="true"]http://openoffice.org/2000/text"[/URL] xmlns:table="[URL unfurl="true"]http://openoffice.org/2000/table"[/URL] xmlns:draw="[URL unfurl="true"]http://openoffice.org/2000/drawing"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format"[/URL] xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:number="[URL unfurl="true"]http://openoffice.org/2000/datastyle"[/URL] xmlns:svg="[URL unfurl="true"]http://www.w3.org/2000/svg"[/URL] xmlns:chart="[URL unfurl="true"]http://openoffice.org/2000/chart"[/URL] xmlns:dr3d="[URL unfurl="true"]http://openoffice.org/2000/dr3d"[/URL] xmlns:math="[URL unfurl="true"]http://www.w3.org/1998/Math/MathML"[/URL] xmlns:form="[URL unfurl="true"]http://openoffice.org/2000/form"[/URL] xmlns:script="[URL unfurl="true"]http://openoffice.org/2000/script"[/URL] office:class="text" office:version="1.0">
	<office:script/>
	<office:font-decls></office:font-decls>
	<office:automatic-styles></office:automatic-styles>
	<office:body>
		<text:sequence-decls>
			<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
			<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
			<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
			<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
		</text:sequence-decls>
		<table:table table:name="My table" table:style-name="My table">
			<table:table-row>
				<table:table-cell table:style-name="My table.B3" table:number-columns-spanned="2" table:value-type="string">
					<text:section text:style-name="Sect1" text:name="Comm1">
						<text:p text:style-name="Standard">comment1</text:p>
					</text:section>
				</table:table-cell>
				<table:covered-table-cell/>
			</table:table-row>
		</table:table>
		<text:section text:style-name="Sect1" text:name="Comm2">
			<text:p text:style-name="Standard">comment2</text:p>
		</text:section>
	</office:body>
</office:document-content>

output

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:script="[URL unfurl="true"]http://openoffice.org/2000/script"[/URL] xmlns:form="[URL unfurl="true"]http://openoffice.org/2000/form"[/URL] xmlns:math="[URL unfurl="true"]http://www.w3.org/1998/Math/MathML"[/URL] xmlns:dr3d="[URL unfurl="true"]http://openoffice.org/2000/dr3d"[/URL] xmlns:chart="[URL unfurl="true"]http://openoffice.org/2000/chart"[/URL] xmlns:svg="[URL unfurl="true"]http://www.w3.org/2000/svg"[/URL] xmlns:number="[URL unfurl="true"]http://openoffice.org/2000/datastyle"[/URL] xmlns:xlink="[URL unfurl="true"]http://www.w3.org/1999/xlink"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format"[/URL] xmlns:draw="[URL unfurl="true"]http://openoffice.org/2000/drawing"[/URL] xmlns:table="[URL unfurl="true"]http://openoffice.org/2000/table"[/URL] xmlns:text="[URL unfurl="true"]http://openoffice.org/2000/text"[/URL] xmlns:style="[URL unfurl="true"]http://openoffice.org/2000/style"[/URL] xmlns:office="[URL unfurl="true"]http://openoffice.org/2000/office">[/URL]
	<comment name="Comm1">
						comment1
					</comment>
	<comment name="Comm2">
			comment2
		</comment>
</root>
 
yep it's the namespace prefix when I changed to
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>
    <xsl:template match="section">
      <comment name="{@name}">
          <xsl:apply-templates/>
       </comment>
    </xsl:template>
</xsl:stylesheet>

it wraped the coment text into propper nodes leaving all the rest of text-trash as-is. when I try to add either select="section" or select="text:section" to the apply-teplates of root elements it cant find such a name and I get empty root :(
 
I copy-pasted your code but I still get empty root. And I did have all xmlns initially. Could it depend on the parse or whatever the thingie that runs xslt is called? Im using java - what do you guys use?
 
I use the following java code - mayhap I forgot something?
Code:
public static void transform(String data, String style, String result, String sys, String pub) {    //transforms source xml file using style xsl file and saves result in result file
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            File stylesheet = new File(style);
            File datafile = new File(data);
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(datafile);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);                 //
            Transformer transformer = tFactory.newTransformer(stylesource);         //ads styling information to the transformation
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");            //sets the encoding declared in xml-file
            if (sys != null) {
                transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, sys); //sets the system dtd
                transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);  //sets the public dtd
            }
            DOMSource source = new DOMSource(document);
            File fout = new File(result);                       //creates output file
            StreamResult result1 = new StreamResult(new PrintWriter(fout, "UTF-8"));   //ensures that file is writen in a certain encoding (should match the encoding declared earlier)
            transformer.transform(source, result1);
}
... lots of catches
 
no looks ok

when i said i used java, i was using jEdits implementation of the XSLT plugin - I cannot see why you are getting empty documents.

Jon?

 
I've no idea. Transform works for me. Have you tried using the exact xml/xsl that flumpy posted?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top