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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:57:42+00:00 2026-06-10T13:57:42+00:00

My XML Code (containing pubs and people): <document> <pubs> <pub id=pub1> <open>true</open> </pub> <pub

  • 0

My XML Code (containing pubs and people):

<document>
    <pubs>
        <pub id="pub1">
            <open>true</open>
        </pub>
        <pub id="pub2">
            <open>false</open>
        </pub>
    </pubs>
    <people>
        <person>
            <name>John</name>
            <pubId>pub1</pubId>
        </person>
        <person>
            <name>Paul</name>
            <pubId>pub2</pubId>
        </person>
    </people>
</document>

uses a reference from <person> to <pub> (person/pubId references pub[@id]). Pubs themselves have a boolean <open> (stating whether the pub is opened or closed).

Using XSLT, I want to be able to count() the number of people that can go to the pub. In order to go to a pub, the pub must have an <open> value of true.

Two possible solutions that I can’t quite code up:

  1. Iterate through the people and keep track of the number:

    <xsl:for-each select="person">
        <xsl:variable name="pId" select="pubId" />
    
        <xsl:if test="root()/document/pubs/pub[@pubId=$pId]/open = 'true'" >
            <!-- any way to keep track of this number? -->
        </xsl:if>
    </xsl:for-each>
    
  2. Use the count() function directly:

    <count>
        <xsl:value-of select="count(//person[root()/document/pubs/pub[@id = pubId]/open = 'true'])" />
    </count>
    

The second option seems more concise and correct, but I can’t get it to work.

Any ideas?

  • 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-10T13:57:44+00:00Added an answer on June 10, 2026 at 1:57 pm

    Use this XPath 1.0 one-liner:

    count(/*/people/person[pubId = /*/pubs/pub[open = 'true']/@id])
    

    This returns the count of person elements in this document that have at least one pubId child such that there is a pub element whose id attribute’s string value equals that pubId and whose open child’s string value is the string "true".

    Here is an XSLT-based verification:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:template match="/">
      <xsl:copy-of select=
          "count(/*/people/person[pubId = /*/pubs/pub[open = 'true']/@id])"/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <document>
        <pubs>
            <pub id="pub1">
                <open>true</open>
            </pub>
            <pub id="pub2">
                <open>false</open>
            </pub>
        </pubs>
        <people>
            <person>
                <name>John</name>
                <pubId>pub1</pubId>
            </person>
            <person>
                <name>Paul</name>
                <pubId>pub2</pubId>
            </person>
        </people>
    </document>
    

    the Xpath expression is evaluated and the result of this evaluation is copied to the output:

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

Sidebar

Related Questions

XML Code: <GridLayout android:id=@+id/grid android:layout_width=fill_parent android:layout_height=fill_parent android:layout_above=@+id/step android:layout_alignParentLeft=true android:layout_alignParentRight=true android:layout_below=@+id/menu > </GridLayout> The above
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I have tons of XML files all containing a the same XML Document, but
I want to convert an java string containing xml to a w3c dom document
Background: We have an XML document containing thousands of pseudocode functions. I've written a
I am trying to parse this document in scala: <?xml version=1.0?> <model> <joint name=pelvis>
I am making a piece of PHP code that takes user XML input (containing
I have some code within struts.xml like: <action name=viewApplicationPDF class=com.xxx.abc.web.action.XApplicationPDFAction> <result name=success>/WEB-INF/templates/xApplication.jsp</result> </action> I
I have my Strings.xml file containing the following code : <?xml version=1.0 encoding=utf-8?> <resources>
I've got some simple code stepping through a large XML file (containing entities defined

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.