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 61k
  • Answers 61k
  • 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 Generally speaking I would use public inheritance, if I want… May 11, 2026 at 9:36 am
  • added an answer It's very unlikely that you will be able to achieve… May 11, 2026 at 9:36 am
  • added an answer As far as I know, there is nothing specific 'web'… May 11, 2026 at 9:36 am

Related Questions

I have an xml file providing data for a datagrid in Flex 2 that
I have a datetime coming back from an XML file in the format: 20080916
I have an XML file and an XML schema in another file and I'd
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an xml file where I need to comment out a whole piece
I have an XML file loaded into a DOM document, I wish to iterate
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an XML file with the following line: <VALUE DECIMAL_VALUE=0.2725 UNIT_TYPE=percent/> I would
I have an xml file from which I am extracting html using LINQ to

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.