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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:31:01+00:00 2026-05-20T09:31:01+00:00

This question is a follow up to my earlier question: Creating a valid XSD

  • 0

This question is a follow up to my earlier question:
Creating a valid XSD that is open using <all> and <any> elements

Given that I have a Java String containing an XML document of the following form:

<TRADE>
  <TIME>12:12</TIME>
  <MJELLO>12345</MJELLO>
  <OPTIONAL>12:12</OPTIONAL>
  <DATE>25-10-2011</DATE>
  <HELLO>hello should be ignored</HELLO>
</TRADE>

How can I use XSLT or similar (in Java by using JAXB) to remove all elements not contained in a set of elements.
In the above example I am only interested in (TIME, OPTIONAL, DATE), so I would like to transform it into:

<TRADE>
  <TIME>12:12</TIME>
  <OPTIONAL>12:12</OPTIONAL>
  <DATE>25-10-2011</DATE>
</TRADE>

The order of the elements is not fixed.

  • 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-20T09:31:01+00:00Added an answer on May 20, 2026 at 9:31 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pNames" select="'|TIME|OPTIONAL|DATE|'"/>
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*" name="identity">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*/*">
      <xsl:if test="contains($pNames, concat('|', name(), '|'))">
       <xsl:call-template name="identity"/>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <TRADE>
        <TIME>12:12</TIME>
        <MJELLO>12345</MJELLO>
        <OPTIONAL>12:12</OPTIONAL>
        <DATE>25-10-2011</DATE>
        <HELLO>hello should be ignored</HELLO>
    </TRADE>
    

    produces the wanted, correct result:

    <TRADE>
       <TIME>12:12</TIME>
       <OPTIONAL>12:12</OPTIONAL>
       <DATE>25-10-2011</DATE>
    </TRADE>
    

    Explanation:

    1. The identity rule (template) copies every node “as-is”.

    2. The identity rule is overridden by a template matching any element that is not the top element of the document. Inside the template a check is made if the name of the matched element is one of the names specified in the external parameter $pNames in a pipe-delimited string of wanted names.

    3. See the documentation of your XSLT processor on how to pass a parameter to a transformation — this is implementation-dependent and differs from processor to processor.

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

Sidebar

Related Questions

This question is a follow-up from How to indicate that a method was unsuccessful
This is somewhat of a follow-up question to this question . Suppose I have
This is a follow up to this question , where I didn't get any
In follow up to this question , it appears that some numbers cannot be
This question is a follow up to my previous question about getting the HTML
This question is a follow up on one of my other questions, Can I
This question is a follow up to: Why can’t I call a method outside
This is a follow up question to This Question . I like (and understand)
This is a follow on to this question . I am trying to avoid
As a follow up to this question: Developing a online exam application, how do

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.