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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:40:12+00:00 2026-05-16T18:40:12+00:00

I’m trying to query XML through XPATH but is having problem getting id() to

  • 0

I’m trying to query XML through XPATH but is having problem getting id() to work.
I would like to get all the authors for a book, specifying the book ID.

Here’s the XML.

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://litemedia.se/BookStore.xsd">
  <book id="ISBN9789170375033">
    <title>I väntan på talibanerna</title>
    <cover>http://image.bokus.com/images2/9789170375033_large</cover>
    <author-ref id="A1" />
  </book>

  <book id="ISBN9789170372063">
    <title>Sista resan till Phnom Penh</title>
    <cover>http://image.bokus.com/images2/9789170372063_large</cover>
    <author-ref id="A1" />
  </book>

  <book id="ISBN9789127121867">
    <title>Vårt bröllop : Kronprinsessan Victoria och Prins Daniel 19 juni 2010</title>
    <cover>http://image.bokus.com/images2/9789127121867_large</cover>
    <author-ref id="A2 A3" />    
  </book>

  <book id="ISBN9789189204966">
    <title>Människa, människa</title>
    <cover>http://image.bokus.com/images2/9789189204966_large</cover>
    <author-ref id="A3" />
  </book>

  <author id="A1">
    <name>Jesper Huor</name>
  </author>
  <author id="A2">
    <name>Susanna Popova</name>
  </author>
  <author id="A3">
    <name>Paul Hansen</name>
  </author>
</bookstore>

And this is my schema.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="BookStore"
    targetNamespace="http://litemedia.se/BookStore.xsd"
    elementFormDefault="qualified"
    xmlns="http://litemedia.se/BookStore.xsd"
    xmlns:mstns="http://litemedia.se/BookStore.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="bookstore">
    <xs:complexType mixed="true">
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="cover" type="xs:anyURI" />
              <xs:element name="author-ref">
                <xs:complexType>
                  <xs:attribute name="id" type="xs:IDREFS"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>

            <xs:attribute name="id" type="xs:ID" />
          </xs:complexType>
        </xs:element>

        <xs:element name="author" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />
            </xs:sequence>

            <xs:attribute name="id" type="xs:ID" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This is my code.

public IList<Author> GetAuthorsForBook(string isbn)
{
    using (var xmlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(XmlDataSourcePath))
    using (var xsdStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(XsdDataSourcePath))
    {
        var doc = new XmlDocument();

        // Load schema
        var schema = XmlSchema.Read(xsdStream, SchemaEvents);
        doc.Schemas.Add(schema);

        // Load document
        doc.Load(xmlStream);

        // Load default namespace: bs
        var nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("bs", "http://litemedia.se/BookStore.xsd");

        // We should be able to do this
        // var path = string.Format("id(id('{0}')/bs:author-ref/@id)", isbn);

        // but this will have to do
        var path = string.Format("/bs:bookstore/bs:author/@id[contains(/bs:bookstore/bs:book[@id='{0}']/bs:author-ref/@id,.)]", isbn);

        return doc.SelectNodes(path, nsmgr).Cast<XmlNode>()
            .Select(node =>
            new Author
            {
                Name = node.FirstChild.Value
            }).ToList();
    }
}

Since I’ve specified bookId and authorId as ID type in the schema I would like to be able to do the following.

var path = string.Format("id(id('{0}')/bs:author-ref/@id)", isbn);

This query always returns 0 elements. If I reduce it to id(‘ISBN9789127121867’) that will also return 0 result, which indicates that id() doesn’t work in my scenario. 🙁

At the moment I’m going for the following query, even if it’s not as efficient as using id() would be, it does give me the results I need.

var path = string.Format("/bs:bookstore/bs:author/@id[contains(/bs:bookstore/bs:book[@id='{0}']/bs:author-ref/@id,.)]", isbn);

Have you ever had a similiar problem or any clue to what I might have done wrong?
Thank you!

Mikael Lundin

  • 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-16T18:40:13+00:00Added an answer on May 16, 2026 at 6:40 pm

    As explained in my comment, schema defined ids don’t matter to XPath 1.0.
    To give you an example how it works with a schema aware XPath 2.0 implementation, here is some code using the XQSharp extension methods on XmlNodes:

        XmlDocument doc = new XmlDocument();
        XmlReaderSettings xrs = new XmlReaderSettings()
        {
            ValidationType = ValidationType.Schema
        };
        xrs.Schemas.Add(null, "schema.xsd");
        using (XmlReader xr = XmlReader.Create("input.xml", xrs))
        {
            doc.Load(xr);
        }
    
        string isbn = "ISBN9789127121867";
    
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("bs", "http://litemedia.se/BookStore.xsd");
    
        string path = string.Format("id(id('{0}')/bs:author-ref/@id)", isbn);
        foreach (XmlElement author in doc.XPathSelectNodes(path, nsmgr))
        {
            Console.WriteLine(author.OuterXml);
        }
    

    When I run that in a project where I have added references to XQSharp and XQSharp.ExtensionMethods and have added a using XQSharp.ExtensionMethods; it outputs the two author elements as e.g.

    <author id="A2" xmlns="http://litemedia.se/BookStore.xsd"><name>Susanna Popova</name></author>
    <author id="A3" xmlns="http://litemedia.se/BookStore.xsd"><name>Paul Hansen</name></author>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but

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.