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

  • Home
  • SEARCH
  • 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 140021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:32:23+00:00 2026-05-11T07:32:23+00:00

I have an xml file in a flat structure. We do not control the

  • 0

I have an xml file in a flat structure. We do not control the format of this xml file, just have to deal with it. I’ve renamed the fields because they are highly domain specific and don’t really make any difference to the problem.

<attribute name='Title'>Book A</attribute> <attribute name='Code'>1</attribute> <attribute name='Author'>    <value>James Berry</value>    <value>John Smith</value> </attribute> <attribute name='Title'>Book B</attribute> <attribute name='Code'>2</attribute> <attribute name='Title'>Book C</attribute> <attribute name='Code'>3</attribute> <attribute name='Author'>     <value>James Berry</value> </attribute> 

Key things to note: the file is not particularly hierarchical. Books are delimited by an occurance of an attribute element with name=’Title’. But the name=’Author’ attribute node is optional.

Is there a simple xpath statement I can use to find the authors of book ‘n’? It is easy to identify the title of book ‘n’, but the authors value is optional. And you can’t just take the following author because in the case of book 2, this would give the author for book 3.

I have written a state machine to parse this as a series of elements, but I can’t help thinking there would have been a way to directly get the results that I want.

  • 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. 2026-05-11T07:32:24+00:00Added an answer on May 11, 2026 at 7:32 am

    We want the ‘attribute’ element of @name ‘Author’ that is following an ‘attribute’ element of @name ‘Title’ with a value of ‘Book n’, without any other ‘attribute’ element of @name ‘Title’ between them (because if there are, then the author authored some other book).

    Said differently, it means we want an author of which the first preceding title (the one it ‘belongs to’) is the one we’re looking for:

    //attribute[@name='Author'] [preceding-sibling::attribute[@name='Title'][1][contains(.,'Book N')]] 

    N=C => finds <attribute name='Author'><value>James Berry</value></attribute>

    N=B => finds nothing

    Using keys and/or grouping functions available in XSLT 2.0 would make this easier (and much faster if the file is big).

    (SO code parser seems to think ‘//’ stands for ‘comments’ but in XPath it’s not!!! Sigh.)

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

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Not 100% percent sure about syntactical details, but something like… May 11, 2026 at 3:36 pm
  • added an answer You can either use DbgView from Sysinternals or add the… May 11, 2026 at 3:36 pm
  • added an answer There's the Java Advanced Imaging API which contains useful stuff… May 11, 2026 at 3:36 pm

Related Questions

I'm designing a system which is receiving data from a number of partners in
I have an ASP.Net application that is hosted on my corporate clients web servers
Suppose a large composite application built on several foundation components packaged in their own
I have an application for which log4j logging is configured in a log4j.properties file.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.