Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Parsing for double elements through xsl

Reply
Thread Tools

Parsing for double elements through xsl

 
 
Linda
Guest
Posts: n/a
 
      03-16-2008
Hi,

Sorry if this is the wrong group I'm posting to. We are using a CMS
which saves all files as .xml. We have an xslt file where we parse
all html tags. At the moment, the CMS sometimes puts in < br / > <
br / > to separate content instead of placing text in < p > tags.

I'm trying to find any instance where a < br > follows a < br >,
however for some reason I'm not getting the syntax quite correct. I'm
just starting out with xsl, so it could be a silly mistake, but I
can't find out a solution.

We use xsl v 1.0. The code that I've tried is:

<xsl:template match="br">

<xsl:choose>
<xsl:when test="preceding-sibling::br[1]"></xsl:when>
<xsltherwise><xsl:copy-of select="."/></xsltherwise>
</xsl:choose>
</xsl:template>

I thought that the [1] is meant to get the immediately preceding
sibling. However it still finds instances where there is a single br.

Can someone help in regards to how to find an immediate preceding
sibling. An example of the code I'm using is:

Text< br />
Text< br />< br />
Text< br />< br />
Text< br />
Text.

It ignores the first < br > instance in the first line (as expected),
but it then finds the last single < br > instance in the 4th line.
Eventually I'm hoping to then move the text in < p > tags (using
preceding-sibling::text()[1]/ following-sibling::text()[1]), but for
the moment I just want to single out the double br.

Regards,
Linda
 
Reply With Quote
 
 
 
 
dnovatchev@gmail.com
Guest
Posts: n/a
 
      03-17-2008
> <xsl:when test="preceding-sibling::br[1]"></xsl:when>

This would select the nearest preceding-sibling element named "br", if
such exists.

You want to test if the nearest preceding-sibling element has the name
"br", and this is essentially different:

Use:

preceding-sibling::*[1][self::br]


Hope this helped.


Cheers,
Dimitre Novatchev



On Mar 16, 3:32*pm, Linda <(E-Mail Removed)> wrote:
> Hi,
>
> Sorry if this is the wrong group I'm posting to. *We are using a CMS
> which saves all files as .xml. *We have an xslt file where we parse
> all html tags. *At the moment, the CMS sometimes puts in < br / > <
> br / > to separate content instead of placing text in < p > tags.
>
> I'm trying to find any instance where a < br > follows a < br >,
> however for some reason I'm not getting the syntax quite correct. *I'm
> just starting out with xsl, so it could be a silly mistake, but I
> can't find out a solution.
>
> We use xsl v 1.0. *The code that I've tried is:
>
> <xsl:template match="br">
>
> * *<xsl:choose>
> * * <xsl:when test="preceding-sibling::br[1]"></xsl:when>
> * * <xsltherwise><xsl:copy-of select="."/></xsltherwise>
> * </xsl:choose>
> </xsl:template>
>
> I thought that the [1] is meant to get the immediately preceding
> sibling. *However it still finds instances where there is a single br.
>
> Can someone help in regards to how to find an immediate preceding
> sibling. *An example of the code I'm using is:
>
> Text< br />
> Text< br />< br />
> Text< br />< br />
> Text< br />
> Text.
>
> It ignores the first < br > instance in the first line (as expected),
> but it then finds the last single < br > instance in the 4th line.
> Eventually I'm hoping to then move the text in < p > tags (using
> preceding-sibling::text()[1]/ following-sibling::text()[1]), but for
> the moment I just want to single out the double br.
>
> Regards,
> Linda


 
Reply With Quote
 
 
 
 
Linda
Guest
Posts: n/a
 
      03-17-2008
Thanks for that, I gave that a try, however in the test, it is still
finding any br, not just the immediate br. Should I be putting this
test within a for-each? or should I be trying to find if the
preceding sibling is text instead?

On Mar 17, 1:04 pm, (E-Mail Removed) wrote:
> > <xsl:when test="preceding-sibling::br[1]"></xsl:when>

>
> This would select the nearest preceding-sibling element named "br", if
> such exists.
>
> You want to test if the nearest preceding-sibling element has the name
> "br", and this is essentially different:
>
> Use:
>
> preceding-sibling::*[1][self::br]
>
> Hope this helped.
>
> Cheers,
> Dimitre Novatchev
>

 
Reply With Quote
 
Peter Flynn
Guest
Posts: n/a
 
      03-17-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> <xsl:when test="preceding-sibling::br[1]"></xsl:when>

>
> This would select the nearest preceding-sibling element named "br", if
> such exists.
>
> You want to test if the nearest preceding-sibling element has the name
> "br", and this is essentially different:
>
> Use:
>
> preceding-sibling::*[1][self::br]


An alternative is preceding-sibling::*[1][local-name()='br']

However, when an XPath statement fails when expected to work (IMHE),
there is a namespace in effect somewhere Check...

>> I thought that the [1] is meant to get the immediately preceding
>> sibling.


Yes, but it's already qualified by the ::br, so your orginal statement
matched the closest preceding element called br (no matter how much
earlier it occurred among the siblings), not the immediately preceding
element. Testing for ::*[1] always finds the immediately preceding
element; you then test for the name.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
 
Reply With Quote
 
dnovatchev@gmail.com
Guest
Posts: n/a
 
      03-17-2008
> Thanks for that, I gave that a try, however in the test, it is still
> finding any br, not just the immediate br. Should I be putting this


Then it's highly likely you might have a default namespace defined and
in
scope.

Search for articles explaining this most FAQ for XPath expressions.

Cheers,
Dimitre Novatchev

"Linda" <(E-Mail Removed)> wrote in message
news:993fdfa5-8024-4c51-96b9-
(E-Mail Removed)...
> Thanks for that, I gave that a try, however in the test, it is still
> finding any br, not just the immediate br. Should I be putting this
> test within a for-each? or should I be trying to find if the
> preceding sibling is text instead?
>
> On Mar 17, 1:04 pm, (E-Mail Removed) wrote:
>> > <xsl:when test="preceding-sibling::br[1]"></xsl:when>

>>
>> This would select the nearest preceding-sibling element named "br", if
>> such exists.
>>
>> You want to test if the nearest preceding-sibling element has the name
>> "br", and this is essentially different:
>>
>> Use:
>>
>> preceding-sibling::*[1][self::br]
>>
>> Hope this helped.
>>
>> Cheers,
>> Dimitre Novatchev
>>


 
Reply With Quote
 
Linda
Guest
Posts: n/a
 
      03-17-2008
Thank you both for the explainations. I didn't think that it had been
defined elsewhere, but I'll let you know if I don't solve the
problem. I've at least got some good directions at what I can search
for now.

Linda

>
> Then it's highly likely you might have a default namespace defined and
> in
> scope.
>
> Search for articles explaining this most FAQ for XPath expressions.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl to group elements? [xsl newbie] Rob Smegma XML 1 09-26-2005 10:59 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 1 09-13-2004 02:28 PM
XSL-1000: (Fatal Error) Error while parsing XSL file (org.apache.xerces.parsers.AbstractSAXParser$AttributesProxy) Kevin Flood Java 0 09-08-2004 02:11 PM



Advertisments