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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:50:28+00:00 2026-06-12T16:50:28+00:00

I have this xml file: <A> <B> <elt>Add</elt> …there are some element here <bId>2</bId>

  • 0

I have this xml file:

<A>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>Add</elt>
    ...there are some element here
    <bId>2</bId>
</B>
<B>
    <elt>can</elt>
    ...there are some element here
    <bId>3</bId>
</B>
<B>
    <elt>can</elt>
    ...there are some element here
    <bId>3</bId>
</B>

I want to check the value of each bId element. If this value is the same with the preceding or following bId element, then I will put the other elements of the bloc B in another bloc excepted the element bId which will be rejected after the transformation. To make my question understanding by you, here is the expected output:

<CA>
  <cplx>
    <spRule>
            <elt>Add</elt>
             ...
    </spRule>
    <spRule>
            <elt>Add</elt>
             ...
    </spRule>
    <spRule>
            <elt>Add</elt>
            ...
    </spRule>
  </cplx>
  <cplx>
    <spRule>
            <elt>can</elt>
            ...
    </spRule>
    <spRule>
            <elt>can</elt>
            ...
    </spRule>
  </cplx>
</CA>

Even though when the element in the xml file are not sorted by the value of bId, I want also get the same expected output.
I try to use this xsl code:

<xsl:for-each select="bId"
  <CA>
    <cplx>
    <xsl:choose>
      <xsl:when test="node()[preceding::bId]">
         <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates/>
         </xsl:copy>
      </xsl:when>
    </cplx>
  </CA>
</xsl:for-each>

but it doesn’t walk. Could someone help me please?
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-12T16:50:29+00:00Added an answer on June 12, 2026 at 4:50 pm

    Assuming your first description to group adjacent elements with the same bId value is what you want an XSLT 1.0 way is as follows:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1"
        match="B[bId = preceding-sibling::B[1]/bId]"
        use="generate-id(preceding-sibling::B[not(bId = preceding-sibling::B[1]/bId)][1])"/>
    
      <xsl:template match="A">
        <CA>
          <xsl:apply-templates select="B[not(preceding-sibling::B[1]) or not(bId = preceding-sibling::B[1]/bId)]"/>
        </CA>
      </xsl:template>
    
      <xsl:template match="B">
        <cplx>
          <xsl:apply-templates select=". | key('k1', generate-id())" mode="sp"/>
        </cplx>
      </xsl:template>
    
      <xsl:template match="B" mode="sp">
        <spRule>
          <xsl:copy-of select="node()[not(self::bId)]"/>
        </spRule>
      </xsl:template>
    
    </xsl:stylesheet>
    

    If you simply want to group all B elements with the same bId value then use

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:key name="k1"
        match="B"
        use="bId"/>
    
      <xsl:template match="A">
        <CA>
          <xsl:apply-templates select="B[generate-id() = generate-id(key('k1', bId)[1])]"/>
        </CA>
      </xsl:template>
    
      <xsl:template match="B">
        <cplx>
          <xsl:apply-templates select="key('k1', bId)" mode="sp"/>
        </cplx>
      </xsl:template>
    
      <xsl:template match="B" mode="sp">
        <spRule>
          <xsl:copy-of select="node()[not(self::bId)]"/>
        </spRule>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml file and I want to get some values with Xpath.
I have thread where downloading xml file a this xml file i want to
I want to use DOM4j for parsing xml file in Java. I have this
I have this XML file , and I want to find these queries :
i have this xml file called data_out.xml, and i want to do DTD and
I have this XML file: <Elements> <Element name=A.B.C.x> <Child>...</Child> <Child>...</Child> <Child>...</Child> </Element> <Element name=A.B.C.y>
To start off, I have this XML file that contains recursive parent-child elements. Here
I have this XML file, which I am trying to deserialize. How can I
I have an XML file that I want to base some unit tests off
I have this xml file. var xml:XML = <rootNode> <element name=CompositeFont> <attribute name=Self> <data

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.