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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can i write xsl file that will just filter XML ???

Status
Not open for further replies.

mooki

Programmer
Aug 1, 2000
32
IL
i need to do a search over xml and get the result in xml.
can i use xsl file for that ?
how can i use it so it will keep the xml format ???
(i tried xsl:eek:utput method="xml" but it did not help much.....)
 
XSL operates on XML files and produces XML output. That sounds like what you want to do.

How about some details?

[li]What do you have?[/li]
[li]What are you trying to do?[/li]
[li]What do you want to achieve?[/li]
 
I guess what you missed are:
<xsl:copy-of>,
<xsl:copy>
and
<xsl:element>
With these instructions you copy or create nodes.
 
I want to use xsl for filtering an xml like a search mechanism. the output should remain in xml.

I managed to copy the xml as is with :

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

i need to combine a search in addition to that.
example :
source:
<START>
<ROW1>
<Name> John </Name>
<Desc> aaaa </Desc>
</ROW1>
<ROW2>
<Name> Mike </Name>
<Desc> bbbb </Desc>
</ROW2>
</START>

i need xsl that will print name,desc where Name=John
<START>
<ROW1>
<Name> John </Name>
<Desc> aaaa </Desc>
</ROW1>
</START>


p.s
thanx for your last responses.

 
First of all: you made it bit more tricky then nescessary by giving all your nodes different tages (ROW1, ROW2).
Why not call them all just ROW, and maybe add an attribute for id's?
Second: in your example there are whitespaces in the node-value. It is not clear if the guy is called "John" or " John ". I guess you don't want them in your filter-expression, so I used the normalize-space function.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/START">
    <xsl:copy>
      <xsl:copy-of select = "*[normalize-space(Name)='John']"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
 
using that xsl i got :
Code:
<?xml version="1.0" encoding="UTF-8"?>

     
        John 
        aaaa 
     
     
        Mike 
        bbbb
 
Strange, I just cheched, and I get:
<?xml version="1.0" encoding="UTF-8"?>
<START>
<ROW1>
<Name> John </Name>
<Desc> aaaa </Desc>
</ROW1>
</START>
As you donst get any tags, I wonder...
Are sure your xml IS parsed? Aren't you just viewing the source, using a browser?
 
That is wierd ! when im using tool like msxsl.exe i do get the right result but when im using it from java
using the following code, i get it wrong
Code:
Source source = new DOMSource(elem);
Result result = new StreamResult(new File(filename));
Source xsl    = new StreamSource(new File("c:\\test.xsl"));
		
TransformerFactory factory=TransformerFactory.newInstance();
Templates template = factory.newTemplates(xsl);
Transformer transformer = template.newTransformer();
	
transformer.transform(source, result);

the thing is it worked before using java and the
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>


what am i doing wrong ????
 
hey,

i got a similar problem. how can i move/copy-of a node to another node. the parent node has got an ID whereas the moving node(childnode) stores the parentID in a child element.

easy to do? I'd appreciate a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top