Unique attributes and preceding-sibling question

J

johkar

I am having trouble figuring out how to use preceding sibling when I am
already on the node. I have a feeling that you cannot use
preceding-sibling on an attribute, but I can't figure out the syntax.

XML:
<leftcol>
<nav type="extra" location="top">
<boxlink href="#">Link 1</boxlink>
</nav>
<nav type="extra" location="bottom">
<boxlink href="#">Link 1</boxlink>
</nav>
<nav type="right">
<boxlink href="#">Link 1</boxlink>
</nav>
</leftcol>

<xsl:apply-templates select="/leftcol/nav" />

<xsl:if test="@type='extra' and not(@type=preceding-sibling::./@type)">
<!-- More XSL -->
</xsl:if>

The goal is to only output one nav with a type of extra.
 
J

Joseph Kesselman

The problem is your use of ., which is not correct syntax.

You want to look at the preceeding <nav>'s type attribute, so:

<xsl:if test="@type='extra' and not(@type=preceding-sibling::nav/@type)">
 
R

roy axenov

johkar said:
XML:
<leftcol>
<nav type="extra" location="top">
<boxlink href="#">Link 1</boxlink>
</nav>
<nav type="extra" location="bottom">
<boxlink href="#">Link 1</boxlink>
</nav>
<nav type="right">
<boxlink href="#">Link 1</boxlink>
</nav>
</leftcol>

<xsl:apply-templates select="/leftcol/nav"/>

<xsl:if test="@type='extra' and
not(@type=preceding-sibling::./@type)">

That's just plain wrong. '.' is a shortcut for the self
axis, so basically you're trying to use two axes at the
same time.
<!-- More XSL -->
</xsl:if>

Generally, if you're asking for help, it's a good idea to
post something that people can stuff into their XSLT
processors right away and see what happens.
The goal is to only output one nav with a type of extra.

So don't use if. *Select* only one nav. Or *match* only one
nav. That's what predicates are for:

<xsl:apply-templates
select="/leftcol/nav[@type='extra'][1]"/>
 
J

johkar

Joseph said:
You want to look at the preceeding <nav>'s type attribute, so:

<xsl:if test="@type='extra' and not(@type=preceding-sibling::nav/@type)">

Thank you for your help.
 
J

johkar

roy said:
So don't use if. *Select* only one nav. Or *match* only one
nav. That's what predicates are for:

<xsl:apply-templates
select="/leftcol/nav[@type='extra'][1]"/>

Thanks for the great tip.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top