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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:07:04+00:00 2026-05-30T21:07:04+00:00

am trying to flatten an XML element based on a child element name using

  • 0

am trying to flatten an XML element based on a child element name using XSLT 1.0

The source XML:

<Contact>
  <ContactPurpose>
    <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
  </ContactPurpose>
  <ContactPurpose>
    <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
  </ContactPurpose>
</Contact>

Should be transformed into the following XML:

<Contact>
  <ContactPurpose>O</ContactPurpose>
  <ContactPurpose>Call</ContactPurpose>
</Contact>

The Logic is:

IF the child element name is “PurposeAsPlainText “ THEN
set “O” for Other in destination

ELSEIF the child element name is “PurposeAsEnum” THEN
copy the source value to destination

EDIT 1: I could be more clear as none of the solutions flattened the xml, please see revised source and dest XML.

EDIT 2: Here is the XML I was testing against. The two tranform solutions below actually do work on my original xml but not the revised xml that I was testing using .NET 4.0 XslCompiledTransform. Or should I make a new question?

<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PatientRecord>
    <Demographics>
      <Contact>
        <ContactPurpose>
          <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
        </ContactPurpose>
        <ContactPurpose>
          <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
        </ContactPurpose>
      </Contact>
    </Demographics>
  </PatientRecord>
</MyDS>
  • 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-30T21:07:05+00:00Added an answer on May 30, 2026 at 9:07 pm

    This can be done in a simple and short way (no explicit conditionals):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:x="cds_dt" exclude-result-prefixes="x">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>
    
     <xsl:template match="ContactPurpose/*"><xsl:apply-templates/></xsl:template>
    </xsl:stylesheet>
    

    when applied to the following XML document (extended to encorporate both cases of interest):

    <Contact>
        <ContactPurpose>
            <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
        </ContactPurpose>
        <ContactPurpose>
            <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
        </ContactPurpose>
    </Contact>
    

    produces the wanted, correct result:

    <Contact>
       <ContactPurpose>0</ContactPurpose>
       <ContactPurpose>Call</ContactPurpose>
    </Contact>
    

    Explanation:

    Overriding the identity rule and appropriate use of templates/match patterns.

    Update: The OP has modified his XML document, which is now in a default namespace:

    <MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <PatientRecord>
            <Demographics>
                <Contact>
                    <ContactPurpose>
                        <PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
                    </ContactPurpose>
                    <ContactPurpose>
                        <PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
                    </ContactPurpose>
                </Contact>
            </Demographics>
        </PatientRecord>
    </MyDS>
    

    Accordingly, here is a slightly modified transformation that produces the wanted result:

    <xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:x="cds_dt" xmlns:c="cds" exclude-result-prefixes="c x">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <xsl:template match="node()|@*">
             <xsl:copy>
               <xsl:apply-templates select="node()|@*"/>
             </xsl:copy>
         </xsl:template>
    
         <xsl:template match="c:ContactPurpose/x:PurposeAsPlainText/text()">0</xsl:template>
    
         <xsl:template match="c:ContactPurpose/*"><xsl:apply-templates/></xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the new XML document (closest above), the new wanted, correct result is produced:

    <MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <PatientRecord>
          <Demographics>
             <Contact>
                <ContactPurpose>0</ContactPurpose>
                <ContactPurpose>Call</ContactPurpose>
             </Contact>
          </Demographics>
       </PatientRecord>
    </MyDS>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to flatten a hierarchy / structure to XML using XSLT 1
I am trying to flatten the xml output of xstream using a converter/marshaling with
I am currently trying to flatten a deep-structured XML document in C# so that
I am trying to flatten out the contents of a source List to fields
I'm trying to flatten out a simple class that has a parent + child
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I am trying to write a script (preferably in bash) to flatten a java
I'm trying to parse a nested array structure of the following form: [element [[child1]
I'm trying to learn XSLT (for some holiday coding fun). I think I now

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.