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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:49:22+00:00 2026-06-13T23:49:22+00:00

I need to transform an XML file by adding a new element that will

  • 0

I need to transform an XML file by adding a new element that will have a value based on data in the current file and two other XML files using XSLT 1.0. The files:

File1:

<Table>
    <Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
    </Row>
    <Row>
        <ColA>AValue2</ColA>
        <ColB>BValue2</ColB>
    </Row>
</Table>

File2:

<Table>
    <Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
        <ColC>CValue1</ColC>
    </Row>
    <Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
        <ColC>CValue1</ColC>
    </Row>
<Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
        <ColC>CValue2</ColC>
    </Row>
    <Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
        <ColC>CValue3</ColC>
    </Row>
    <Row>
        <ColA>AValue2</ColA>
        <ColB>BValue2</ColB>
        <ColC>CValue1</ColC>
    </Row>
</Table>

File3:

<Table>
    <Row>
        <ColC>CValue1</ColC>
        <ColD>ABC</ColD>
    </Row>
    <Row>
        <ColC>CValue2</ColC>
        <ColD>DEF</ColD>
    </Row>
    <Row>
        <ColC>CValue3</ColC>
        <ColD>DEF</ColD>
    </Row>
</Table>

Rows in File1 have a one-to-many relationship with rows in File2 by ColA and ColB.

Rows in File2 have a many-to-one relationship with rows in File3 by ColC.

For each row in File1, I need to:

  1. Look up distinct ColC values in File2 for rows matching on ColA and ColB

  2. For each distinct ColC value, look up ColD value in File3 for rows matching on ColC

  3. Count the number of occurences of ColD values looked up in step 2. ColD will have one of two values (say “ABC” or “DEF”). I need to know if there are more “ABC” than “DEF” and if so add <ColD>ABC</ColD> to that Row in File1. Otherwise add <ColD>DEF</ColD> to that Row in File1. At the end, each Row in File1 should have <ColD>ABC</ColD> or <ColD>DEF</ColD>.

Desired Result (File1 transformed):

<Table>
    <Row>
        <ColA>AValue1</ColA>
        <ColB>BValue1</ColB>
        <ColD>DEF</ColD>
    </Row>
    <Row>
        <ColA>AValue2</ColA>
        <ColB>BValue2</ColB>
        <ColD>ABC</ColD>
    </Row>
</Table>

<ColD>DEF</ColD> would be added to the first Row since there were two occurences of DEF compared to 1 (distinct) occurence of ABC. <ColD>ABC</ColD> would be added to Row 2 since there was one occurence of ABC and zero DEF.

  • 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-13T23:49:25+00:00Added an answer on June 13, 2026 at 11:49 pm

    I came up with a solution that appears to work (haven’t tested it very much). I’m retyping it here so beware of typos. The thing I had the hardest time with was figuring out how to select unique ColC values. Looking at the solution again I didn’t need unique values. I had it in my mind that I would loop thru $unique_c_values to count ColD values, but it turned out differently.

    <xsl:template match="node()|@">
        <xsl:copy>
            <xsl:apply-templates select="node()|@">
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Row">
        <xsl:copy>
            <xsl:apply-templates select="node()|@">
    
            <xsl:variable name="unique_c_values" select=
                "document('file2.xml')/Table/Row[current()/ColA = ColA and current()/ColB = ColB]/ColC[not(. = preceding-sibling::*/ColC[current()/ColA = ../ColA and current()/ColB = ../ColB])]"/>
    
            <xsl:variable name="d_values" select=
                "document('file3.xml')/Table/Row[ColC = $unique_c_values]/ColD"/>
    
            <xsl:choose>
                <xsl:when test="count($d_values[. = 'DEF']) &gt; count($d_values[. = 'ABC'])">
                    <ColD>DEF</ColD>
                </xsl:when>
                <xsl:otherwise>
                    <ColD>ABC</ColD>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I need to transform using an XSL script.
I have xml/rdf file which I get from service. So, I need to transform
I need to split the following XML file based on a predetermined value, for
I have an XML file that I need to sort. Worked great until the
I need to develop an application that will periodically check data from a XML
I have successfully been able to transform a simple xml file with data to
I am developing an application were I need to transform XML documents that look
I need to transform an XML file to another XML file, where the source
I have a requirement to run a transform on a xml file. it is
I need to have my XSLT stylesheet sort my XML file's child nodes, but

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.