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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:21:44+00:00 2026-06-14T22:21:44+00:00

Here is the xml I am working with and I am trying to the

  • 0

Here is the xml I am working with and I am trying to the the id number of the first different pd value I encountered.

<result>
  <pd>a</pd>
  <id>1</id>
  <pd>a</pd>
  <id>2</id>

  <pd>b</pd>
  <id>8</id>

  <pd>c</pd>
  <id>9</id>
  <pd>c</pd>
  <id>4</id>

  <pd>d</pd>
  <id>7</id>
</result>

so the result that I am expecting is:

 <pd>a</pd>
 <id>1</id>

 <pd>b</pd>
 <id>8</id>

 <pd>c</pd>
 <id>9</id>

 <pd>d</pd>
 <id>7</id>

I used following-sibling::*[2] to get to the next pd and compare that with the current node but then it only gave me b, not c and d. Can any of you give me a hint on how to iterate through the whole xml to get the desired output? 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-06-14T22:21:45+00:00Added an answer on June 14, 2026 at 10:21 pm

    There may be more elegant solutions, but the strategy I took was to gather all of the distinct values in the pd elements, then loop through all of the pd elements and grab the first sibling where the value in the pd element matched the value in the current iteration of the distinct values.

    for $distinct-pd in fn:distinct-values($xml//pd)
      for $pd in $xml//pd[./text() eq $distinct-pd][1]
        return ($pd, $pd/following-sibling::*[1])
    

    For your code, this yields

    <pd>a</pd>
    <id>1</id>
    <pd>b</pd>
    <id>8</id>
    <pd>c</pd>
    <id>9</id>
    <pd>d</pd>
    <id>7</id>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code first rates.xml <?xml version=1.0 encoding=UTF-8?> <main> <unittype id=24944> <date value=2020-10-10>
here is the xml data : <root> <recommendedMaterials> <value label=Aluminium data=0 /> <value label=Iron
I'm trying to learn some XML Parsing here and I've been given some code
I am trying to implement the following: Please check the image here: The problem
I'm trying to write my first Android app. It will take a number input
I am trying to write a function that will take an xml object, an
I'm trying to make the text all centered but it's not working out. Here's
I'm new to working with xml and xsd stuff. I'm trying to create a
I am working on a page that uses XPATH to traverse an XML document
I am trying load settings.php?id=5 by accessing xml/5/settings.xml here is the code but its

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.