Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3300914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:42:13+00:00 2026-05-17T20:42:13+00:00

Using XSLT I’m trying to work out a way to only emit a HTML

  • 0

Using XSLT I’m trying to work out a way to only emit a HTML table row when a value is different from the last iteration of the for-each loop. Essentially I want to make the table appear to have grouped headers, by only writing out the header once it changes.

I’m having a bit of trouble conceptually working out how I could do that considering you can’t change the value of a variable once it’s been defined.

<xsl:for-each select="//databaseConstraint/constraint">

    <xsl:variable name="caption" select="@caption" />

    <tr>
        <th colspan="2"><xsl:value-of select="$caption" /></th>
    </tr>

    ...

</xsl:for-each >

Update I’ve tried below, but I get an error: NodeTest expected here

<xsl:for-each select="//databaseConstraint/constraint">

    <xsl:variable name="caption" select="@caption" />

    <xsl:if test="not(@caption = preceding-sibling::@caption)">
    <tr>
        <th colspan="2">
            <xsl:value-of select="$caption" />
        </th>
    </tr>
    </xsl:if>

    ...

</xsl:for-each >
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:42:14+00:00Added an answer on May 17, 2026 at 8:42 pm
    <xsl:if test="not(@caption = preceding-sibling::@caption)">
    

    This would test whether the caption is equal to a caption attribute that is a sibling of the context node, i.e. a sibling of the constraint element that you’re processing. But I suspect that it “fails” with a syntax error, because the caption step has two axes: preceding-sibling::, and attribute:: (which @ is short for).

    What you probably want is

    <xsl:if test="not(@caption = preceding-sibling::constraint[1]/@caption)">
    

    This will do what you want, it’s simpler than Muenchian, and it’s probably speedy enough, if the browser’s XPath implementation is decent, because it only needs to test one other node, rather than all preceding constraint nodes.

    If this strategy is not fast enough for your purposes, e.g. if you have a lot of data, you can use Muenchian grouping, as @Frédéric said.

    Addition: the [1] is an abbreviation for [position() = 1]. What it means here is that on the right-hand side of the =, we only have the @caption of constraint immediately preceding the current element. If we omitted the [1], we would be comparing the current element’s @caption value with the @captions of all preceding sibling constraint elements.

    It’s important to realize that the XPath = operator operates on node sets (when given a chance), not just on single values or nodes. So A = B, where A and B are nodesets, returns true if there is any member of nodeset A that is equal to any member of nodeset B. This is rather like a join in SQL. It’s a powerful operation, but you have to be aware of what it’s doing.

    One other detail… Why does the [1] yield the constraint element immediately preceding the current one, instead of the first one in the document? Because position() reflects the direction of the current axis, which in this case is preceding-sibling. As the XPath spec says,

    An
    axis that only ever contains the
    context node or nodes that are before
    the context node in document order is
    a reverse axis. Thus, the ancestor,
    ancestor-or-self, preceding, and
    preceding-sibling axes are reverse
    axes; all other axes are forward axes.
    …

    The proximity position of a member of
    a node-set with respect to an axis is
    defined to be the position of the node
    in the node-set ordered in document
    order if the axis is a forward axis
    and ordered in reverse document order
    if the axis is a reverse axis. The
    first position is 1.

    HTH.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to output html controls using xslt, but I need to be able
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using XSLT 1.0 - I have the following xml and I an trying to
Using XSLT, how can I wrap siblings that share the same value for an
I am using XSLT to generate my HTML. I am having below xml and
I am using XSLT to recurse some XML and then apply some HTML to
I am using xslt to transform an xml document to html for use in
I use VS 2008, .net 3.5 for generate page html using XSLT. I have
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
I am parsing a document using XSLT. Within the XSLT I am loading a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.