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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:18:28+00:00 2026-05-20T05:18:28+00:00

I have to transform xml file where i should check field id ‘0’, field

  • 0

I have to transform xml file where i should check field id ‘0’, field id ‘1’ and sum field id ‘2’. For example I have:

<document>
    <line id="0">
        <field id="0">MAR</field>
        <field id="1">doc1</field>
        <field id="2">2</field>
    </line>
    <line id="1">
        <field id="0">MAR</field>
        <field id="1">doc2</field>
        <field id="2">3</field>
    </line>
    <line id="2">
        <field id="0">AAA></field>
        <field id="1">doc4</field>
    </line>
    <line id="3">
        <field id="0">MAR</field>
        <field id="1">doc1</field>
        <field id="2">4</field>
    </line>
</document>

result should be:

<type-MAR>
    <document>doc1</document>
    <sum>6</sum>
    </type-MAR>   
<type-MAR>
    <document>doc2</document>
    <sum>3</sum>
</type-MAR>

there I should take all MAR lines, and show some results which are depends of field id ‘1’.

My idea was, first off all do cycle(for each) and use condition(when). Maybe somebody offer more omptimal decision.

I add new note, how to check if data comes like that:

<field id="0">MAR999</field>
<field id="1">doc1-1231</field>

First field i try to use function contains ‘MAR’, others substring-before ‘-‘. but I stuck when I try it use on Yours program. maybe you can take some advice for it?

  • 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-20T05:18:29+00:00Added an answer on May 20, 2026 at 5:18 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:key name="kLineById0Id1" match="line[field[@id=2]]"
      use="concat(field[@id=0],'+',field[@id=1])"/>
    
     <xsl:template match=
      "line[field[@id=2]
          and
            generate-id()
           =
            generate-id(key('kLineById0Id1',
                            concat(field[@id=0],
                                   '+',field[@id=1])
                           )[1])
           ]
      ">
      <xsl:element name="type-{field[@id=0]}">
           <document>
            <xsl:value-of select="field[@id=1]"/>
           </document>
           <sum>
            <xsl:value-of select=
            "sum(key('kLineById0Id1',
                    concat(field[@id=0],
                           '+',field[@id=1])
                   )
                   /field[@id=2]
               )
            "/>
           </sum>
         </xsl:element>
     </xsl:template>
    
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <document>
        <line id="0">
            <field id="0">MAR</field>
            <field id="1">doc1</field>
            <field id="2">2</field>
        </line>
        <line id="1">
            <field id="0">MAR</field>
            <field id="1">doc2</field>
            <field id="2">3</field>
        </line>
        <line id="2">
            <field id="0">AAA></field>
            <field id="1">doc4</field>
        </line>
        <line id="3">
            <field id="0">MAR</field>
            <field id="1">doc1</field>
            <field id="2">4</field>
        </line>
    </document>
    

    produces the wanted, correct result:

    <type-MAR>
       <document>doc1</document>
       <sum>6</sum>
    </type-MAR>
    <type-MAR>
       <document>doc2</document>
       <sum>3</sum>
    </type-MAR>
    

    Explanation: The Muenchian method for grouping is used with the key defined as the concatenation of two elements.

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

Sidebar

Related Questions

I'm trying to have a script automatically transform an xml file into several html
In my Python app, I have an XML document that I'd like to transform
I have written code in C# that should transform an XML with the help
I have an XSLT transform I am using to process an XML file, inserting
we have XSLT to transform XML into HTML in XHTML 1.0 Strict in XSLT
I have the following file structure (XML files 'index.xml' in nested folders): index.xml foo/index.xml
As the title says it, I have a huge xml file (GBs) <root> <keep>
I have an xslt script that transforms an xml file to another xml file.
Let's assume I have a simple XML file: <data> <text>Hello world!&lt;br&gt;Nice to see you
I need to transform an XML file to another XML file, where the source

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.