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 915893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:57:50+00:00 2026-05-15T17:57:50+00:00

I have a particular problem that I can’t seem to solve. Is it possible

  • 0

I have a particular problem that I can’t seem to solve.

Is it possible to select all nodes using xpath and xslt without the use of additional templates or for-each?

Example xml:

<aaa id="11">
    <aaa id="21"></aaa>
    <bbb id="22">
        <aaa id="31"></aaa>
        <bbb id="32"></bbb>
        <ccc id="33"></ccc>
        <ddd id="34"></ddd>
        <ddd id="35"></ddd>
        <ddd id="36"></ddd>
    </bbb>
    <ccc id="23"></ccc>
    <ccc id="24"></ccc>
</aaa>

A user has the ability to type in an xpath expression through a form, such as:

//aaa/bbb/ddd/@id

The user would expect to receive the ids from:

<ddd id="34"></ddd>
<ddd id="35"></ddd>
<ddd id="36"></ddd>

Outputting:

34 35 36

The only ways I have been able to achieve this is by using additional templates and for-each:

For-each way:

<xsl:template match="/">
    <html>
        <body>
            <xsl:for-each select="//aaa/bbb/ddd">
                <tr>
                    <td>
                        <xsl:value-of select="@id" />
                    </td>
                </tr>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

Additional template way:

    <xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="//aaa/bbb/ddd">
    <xsl:value-of select="@id"/>
</xsl:template>

Each of these examples require extra work to detach the @id from the original expression. I would like to use the user inputted expression as is, and just plug it in somewhere.

I have tried the following, which I thought would select all, but it only returns the first instance:

<xsl:template match="/">
    <html>
        <body>
            <xsl:value-of select="//aaa/bbb/ddd/@id"/>
        </body>
    </html>
</xsl:template>

Is there a solution to my problem (i.e. a way to just plug in the user inputted expression as is?)

EDIT: Note – I need a solution that will work with any xpath expression given by the user.. no matter how complex.

Let me know if you need any further clarification.. I tried my best to explain it, but maybe I didn’t do that very well.. Thank you in advance for your patience!

Thanks! 🙂

  • 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-15T17:57:50+00:00Added an answer on May 15, 2026 at 5:57 pm

    With this stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="node-set" select="/aaa/bbb//ddd/@id"/>
        <xsl:template match="/">
            <html>
                <body>
                    <table>
                        <tr>
                            <th>Type</th>
                            <th>Name</th>
                            <th>Value</th>
                        </tr>
                        <xsl:apply-templates select="$node-set" mode="result"/>
                    </table>
                </body>
            </html>
        </xsl:template>
        <xsl:template match="@*|node()" mode="result">
            <tr>
                <td>
                    <xsl:choose>
                        <xsl:when test="self::*">Element</xsl:when>
                        <xsl:when test="self::text()">Text</xsl:when>
                        <xsl:when test="self::comment()">Comment</xsl:when>
                        <xsl:when test="self::processing-instruction()">PI</xsl:when>
                        <xsl:when test="count(.|/)=1">Root</xsl:when>
                        <xsl:when test="count(.|../@*)=count(../@*)">Attribute</xsl:when>
                        <xsl:when test="count(.|../namespace::*)=count(../namespace::*)">Namespace</xsl:when>
                    </xsl:choose>
                </td>
                <td>
                    <xsl:value-of select="name()"/>
                </td>
                <th>
                    <xsl:value-of select="."/>
                </th>
            </tr>
        </xsl:template>
    </xsl:stylesheet>
    

    Result:

    <html>
        <body>
            <table>
                <tr>
                    <th>Type</th>
                    <th>Name</th>
                    <th>Value</th>
                </tr>
                <tr>
                    <td>Attribute</td>
                    <td>id</td>
                    <th>34</th>
                </tr>
                <tr>
                    <td>Attribute</td>
                    <td>id</td>
                    <th>35</th>
                </tr>
                <tr>
                    <td>Attribute</td>
                    <td>id</td>
                    <th>36</th>
                </tr>
            </table>
        </body>
    </html>
    

    Note: It would be an error if $node-set isn’t a node set.

    Edit: Added complete node type test in order to prove that this stylesheet works with any XPath expression wich eval to a node set.

    Edit 2: Added template/@mode in order to not miss root.

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

Sidebar

Related Questions

I have a problem that can be simplified as follows: I have a particular
I got this problem that I can't just solve algorithmically. Let's say i have
I have a problem; I'm using an external library where one particular event has
I have a particular problem, I have some program that I cannot modify but
In the end, I have decided that this isn't a problem that I particularly
I have a problem downloading particular file types by WebClient. So there are no
I have a problem with <h:inputText> . In particular I have a series of
I have web-service which give list of property at a particular area My problem
I have problem pertaining to the maintaining state of the particular @Request scope bean.
i have this problem to find a particular xml node l have post this

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.