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

Xpath expression to extract content from a node but not its child node

Status
Not open for further replies.
Apr 19, 2006
4
GB
Hi guys

i've got an xml file which is structured like below

<intro>
<abstract>here's my abstract</abstract>
and here is my intro
</intro>


I want to be able to extract "and here is my intro" from this xml file - without extracting the contents of the abstract node.... Can anyone give me any ideas as to how to do this?

I guess I need the correct xpath expression, but I can't seem to figure out one to use!

DOCUMENT/INTRO - extracts both, DOCUMENT/INTRO/ABSTRACT extracts just the abstract... How do I extract just the intro and not the childnode abstract?

Many thanks for any ideas

Joe
 
With <intro> in the context, use
[tt]<xsl:value-of select="text()" />[/tt]
 
That works, however if i do that my "template match" rules will not apply to that node

I'm using template match to replace <br/> tags with actual new lines for a textarea......

e.g.
<xsl:template match="DOCUMENT/INTRO/br">
.....
</xsl:template>


<xsl:template match="DOCUMENT/INTRO/text()/br"> doesn't work.....

any ideas?
 
tsuji's response was correct for your question. Try to define the actual problem a little better.

Jon

"I don't regret this, but I both rue and lament it.
 
Basically doing what tsuji has suggested has worked but has caused another problem.

Which is that i've lost the ability to convert <br/> tags within my node to new lines.

At the top of my file I do a template-match to convert all <br/> tags into new lines

<xsl:template match="/DOCUMENT/*/br">
<xsl:text>&#xa;</xsl:text>
</xsl:template>

when I display the contents of DOCUMENT/INTRO/text() my template match above will not work and it will display all the text in one big chunk.....

Any ideas?
 
I can vague see what you are getting at. But you can spare the labour of replacing br-tag. In fact you can leave out any style-related such as br-tag or content-related such as abstract-tag if the structure is consistently like what you said. In your case, if you're sure it is only involving br-tag, you can do it simply like this. Take an example for fun.

[tt]<?xml version="1.0" encoding="utf-8"?>
<document>
<intro>
<abstract>Let C be a supersingular genus-2 curve over an algebraically closed field of characteristic 3. We show that if C is not isomorphic to the curve y^2 = x^5 + 1 then up to isomorphism there are exactly 20 degree-3 maps phi from C to the elliptic curve E with j-invariant 0. We study the coarse moduli space of triples (C,E,phi), paying particular attention to questions of rationality. The results we obtain allow us to determine, for every finite field k of characteristic 3, the polynomials that occur as Weil polynomials of supersingular genus-2 curves over k.</abstract>
Over an algebraically closed field of characteristic 3, every supersingular genus-2
curve C has a degree-3 map to the supersingular elliptic curve E with j-invariant 0.<br />We study the coarse moduli space A of such maps C -> E, paying special attention
to questions of rationality.<br />We show that with a single exception, every supersingular genus-2 curve over an algebraically-closed field of characteristic 3 has 20
non-isomorphic maps to the elliptic curve with j-invariant 0, and we provide explicit equations for these maps.
</intro>
</document>
[/tt]
You can send it to the textarea like this.
[tt]
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="html" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="document/intro">
<html>
<body>
<form>
<textarea rows="15" cols="72">
<xsl:for-each select="text()">
<xsl:value-of select="normalize-space(.)" /><xsl:text>&#x0a;</xsl:text>
</xsl:for-each>
</textarea>
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/tt]
Hope you get the idea. It is pragmatic in the approach without much ambition, but it works.
 
Thanks very much for your help, normalize-space works a treat,
it's very much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top