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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:16:02+00:00 2026-05-29T15:16:02+00:00

I have an XML document that has several <item> elements. Inside each, there might

  • 0

I have an XML document that has several <item> elements. Inside each, there might be one or more of the following elements: <list>, <listAfter>, and <listBefore>. So, ignoring a lot of the extraneous elements, it might look like this:

<items>
  <item>
    <!-- ... various elements ... -->
    <list>Enhancements</list>
  </item>
  <item>
    <!-- ... various elements ... -->
    <listBefore>Enhancements</listBefore>
    <listAfter>Bugs</listAfter>
  </item>
  <item>
    <!-- ... various elements ... -->
    <list>Enhancements</list>
    <listAfter>Next Release</listAfter>
  </item>
  <item>
    <!-- ... various elements ... -->
    <listBefore>Bugs</listBefore>
  </item>
  <item>
    <!-- ... various elements ... -->
  </item>
</items>

I want to remove all of the extraneous <list*> elements and have one <list> element per <item>. That element’s value should follow this logic:

  • Use the value of <list> if it’s available.
  • Otherwise, use the value of <listAfter> if it’s available.
  • Otherwise, use the value of <listBefore> if it’s available.
  • If none of these fields exist, use No List as the value.

Using my XML document above, here’s what I would expect the output to look like:

<items>
  <item>
    <!-- ... various elements ... -->
    <list>Enhancements</list>
  </item>
  <item>
    <!-- ... various elements ... -->
    <list>Bugs</list>
  </item>
  <item>
    <!-- ... various elements ... -->
    <list>Enhancements</list>
  </item>
  <item>
    <!-- ... various elements ... -->
    <list>Bugs</list>
  </item>
  <item>
    <!-- ... various elements ... -->
    <list>No List</list>
  </item>
</items>

Other than using the Identity Transform to copy over all other elements, I’m not sure how to include this logic in a nice manner. As always, your help is much appreciated.

  • 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-29T15:16:03+00:00Added an answer on May 29, 2026 at 3:16 pm

    You could do this by overriding the identity template, and adding extra templates to match the criteria for the various list elements.

    To match listAfter you wish to include in the output, you would do the following (i.e listAfter elements with no list element as a sibling)

    <xsl:template 
       match="listAfter[not(preceding-sibling::list|following-sibling::list)]">
    

    For the listBefore, you need to match them only if they have neither list not listAfter elements as siblings

    <xsl:template 
      match="listBefore[not(
        preceding-sibling::list|following-sibling::list
        |preceding-sibling::listAfter|following-sibling::listAfter)]">
    

    In other cases, you would ignore listAfter and listBefore elements:

    <xsl:template match="listAfter|listBefore" />
    

    Finally, you can match item elements with none of the various list elements as children like so:

    <xsl:template match="item[not(list|listAfter|listBefore)]">
    

    So, given the following XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes" />
    
       <xsl:template match="listAfter[not(preceding-sibling::list|following-sibling::list)]">
          <list>
          <xsl:apply-templates select="@*|node()"/>
          </list>
       </xsl:template>
    
       <xsl:template match="listBefore[not(preceding-sibling::list|following-sibling::list|preceding-sibling::listAfter|following-sibling::listAfter)]">
          <list>
          <xsl:apply-templates select="@*|node()"/>
          </list>
       </xsl:template>   
    
       <xsl:template match="listAfter|listBefore" />
    
       <xsl:template match="item[not(list|listAfter|listBefore)]">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
          <list>No List</list>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    When this is applied to your source XML, the following is output:

    <items>
       <item>
          <!-- ... various elements ... -->
          <list>Enhancements</list>
       </item>
       <item>
          <!-- ... various elements ... -->
          <list>Bugs</list>
       </item>
       <item>
          <!-- ... various elements ... -->
          <list>Enhancements</list>
       </item>
       <item>
          <!-- ... various elements ... -->
          <list>Bugs</list>
       </item>
       <item>
          <!-- ... various elements ... -->
          <list>No List</list>
       </item>
    </items>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that has a collection of objects. Each object has
I have a simple xml document that looks like the following snippet. I need
I have an XML document that has dates in standard ISO 8601 format. Like
I have an XML document and associated schema that defines several attributes as having
Let's say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword>
I have a root element in my output xml document that has no attributes:
I'm parsing an XML document that has nodes like the following: <objects> <dog> <data1>...</data1>
I have an XML document that has a TextBlock that contains HTML code. <TextBlock>
I have several id that has the following name test1, test2, test3, test4 and
I have a xml document that has a record set like this. <document> <row>

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.