logo
down
shadow

Merging similar attributes with XSLT


Merging similar attributes with XSLT

Content Index :

Merging similar attributes with XSLT
Tag : xml , By : Paul Schwarz
Date : November 24 2020, 01:01 AM

seems to work fine One way to achieve this would be just to remove all f nodes that have following siblings with the same value for form and that have no child element new:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" doctype-public="XSLT-compat" 
    omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="f[not(new) and @form = following-sibling::f/@form]"/>
</xsl:transform>
<forms>
 <f form="AGAtam">
  <na>
     <acc/>
     <sg/>
     <mas/>
  </na>
  <s stem="AGAta"/>
  <m meaning="slayer, killer"/>
 </f>
 <f form="Adatte">
   <new/>
   <s stem=""/>
   <m meaning="he seizes, carries away, takes to a different place"/>
 </f>
</forms>
<xsl:template match="f[not(new) and @form = following-sibling::f/@form]"/>

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to fix this xml node merging to keep all the unique attributes using xslt?


Tag : xml , By : Eric
Date : March 29 2020, 07:55 AM
this one helps. You've asked a number of similar questions. Maybe it's time to buy a book and spend a couple of days reading? This is probably the last time I am going to answer a similar question from you. You should be able to learn from answers so that you never have to ask similar questions. If you are not learning, you have to ask yourself: "What is holding you back?"
Any way, lets have a look at this style-sheet. In the explanation, I will refer to points as numbers within xml or xpath comments. For example point 1 is demarcated or if inside an xpath expression, then (: 1 :) . Obviously, remove the comments for production.
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:John="http://stackoverflow.com/questions/11463900"
  exclude-result-prefixes="xsl xs fn John">
<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="*[starts-with(local-name(),'shelf')]   (: 1 :)">
  <xsl:copy>
   <xsl:apply-templates select="@*" />

    <!-- 2 -->
   <xsl:apply-templates select="
     book[@action='extend']                                  (: 3 :)
         [not( preceding-sibling::book[@action='borrow'])]   (: 4 :)" />

    <!-- 5 -->
   <xsl:for-each-group
    select="book[@action='borrow']     (: 6 :)
             |                         (: 7 :)
            book[@action='extend']
                [preceding-sibling::book[@action='borrow']]   (: 8 :)"
    group-starting-with="book[@action='borrow']">
     <xsl:for-each select="current-group()[1]">
      <xsl:copy>                                            <!-- 9 -->
       <xsl:apply-templates select="@*" />
        <xsl:call-template name="merge-books-deeply">       <!-- 10 -->
         <xsl:with-param name="books" select="current-group()" />
         <xsl:with-param name="name-path" select="()" />
        </xsl:call-template>
      </xsl:copy>
     </xsl:for-each>     
   </xsl:for-each-group>

   <xsl:apply-templates select="                              (: 11 :)
     node()[ not( self::book[@action=('borrow','extend')])]" />

  </xsl:copy>
 </xsl:template>

<xsl:function name="John:children-on-path" as="element()*">
 <xsl:param name="base" as="element()*" />     <!-- 12 -->
 <xsl:param name="path" as="xs:string*" />     <!-- 13 -->
 <xsl:choose>
  <xsl:when test="fn:empty($base)">
   <xsl:sequence select="()" />
  </xsl:when>
  <xsl:when test="fn:empty($path)">
   <xsl:copy-of select="$base/*" />           <!-- 14 -->
  </xsl:when>
  <xsl:otherwise>
   <xsl:sequence select="John:children-on-path(
     $base/*[name()=$path[1]],                      (: 15 :)
     $path[position() ne 1])" /> 
  </xsl:otherwise>  
 </xsl:choose>
</xsl:function>

<xsl:template name="merge-books-deeply">
 <xsl:param name="books" as="element()*" />
 <xsl:param name="name-path" as="xs:string*" />
  <xsl:for-each-group
      select="John:children-on-path($books,$name-path)"
      group-by="name()">                                <!-- 16 -->                 
   <xsl:for-each select="current-group()[last()]" >     <!-- 17 -->
    <xsl:copy>
     <xsl:apply-templates select="@*" />
        <xsl:call-template name="merge-books-deeply">   <!-- 18 -->
         <xsl:with-param name="books" select="$books" />
         <xsl:with-param name="name-path" select="$name-path,name()" />
        </xsl:call-template>
     <xsl:apply-templates select="text()" />        
    </xsl:copy>
   </xsl:for-each>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

Merging two bower.json files with similar attributes (merging two JSON files with similar attributes)


Tag : javascript , By : johntynan
Date : March 29 2020, 07:55 AM
may help you . I want to merge two bower.json files. One is local to my project and the other one is shared between different projects. I need to merge these two files before running bower install. , OK, I wrote the function myself:
var mergeJSON = function (local, shared) {
    var check = true;
    for(var i in shared ) {
        if(typeof(shared[i]) == 'object') {
            for(var j in shared[i]) {
                check =true;
                for (var z in local[i]) {
                    if(j == z){
                        check = false;
                            if( cmp(shared[i][j], local[i][j])>0 ) {
                            //For upgrading the version of dependencies from shared to local
                            grunt.log.writeln("Updating local " + j + " from " + local[i][j] + " to >" + shared[i][j]);
                            local[i][j] = shared[i][j];
                            }else if(cmp(shared[i][j], local[i][j])<0){
                                grunt.log.writeln(j + " in shared bower.json " + " is " + shared[i][j] + " and older than local version " + local[i][j]);

                            }
                    }
                }
                if(check){
                    //add the dependency if does not exist in local
                    local[i][j] = shared[i][j];
                    grunt.log.writeln("Added dependency: " + j + ": " + local[i][j] );
                }
            }
        }
    }
    return JSON.stringify(local,null,4);
};
 grunt.task.registerTask('merge','A task to merge local and shared bower.json file', function(){

            var shared = grunt.file.readJSON("/path/to/sharedJSON");
            var local = grunt.file.readJSON("bower.json");
            grunt.file.write('bower.json',mergeJSON(local,shared));
    });
$ grunt merge

xslt- XSLT 1.0 to combine selected attributes and make the attribute muti-valued, retaining the other attributes


Tag : xml , By : Bado
Date : March 29 2020, 07:55 AM
this will help This is my XML- , You have too much code. All you need is:
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="document">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <cost>
             <xsl:for-each select="cost">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </cost>
        <shipping>
            <xsl:for-each select="shipping">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                    <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </shipping> 
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="@* | *[not (self::cost or self::shipping)]"/>

Merging attributes with XSLT


Tag : xml , By : Pancilobak
Date : March 29 2020, 07:55 AM
To fix this issue I'm an absolute newbie at XSLT, and have cobbled together the following over the last couple of hours with the help of google. So be warned... , Declare a global variable bound to the referenced document:
<xsl:variable name="importee" select="document(//Import/@Project)"/>
<xsl:template match="msb:Import">
    <xsl:copy-of select="$importee/msb:Project/*"/>
</xsl:template>
<xsl:template match="msb:Projects">
  <xsl:copy>
    <xsl:copy-of select="@*"/> 
    <xsl:attribute name="InitialTargets" select="$importee/msb:Projects/@InitiaTargets"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Merging specific nodes and adding up attributes in XSLT from XML


Tag : xml , By : DonMac
Date : March 29 2020, 07:55 AM
will help you For an ordered output in XSLT-1.0, you have to apply the Muenchian Grouping method. There are many examples for this approach on SO.
Applied to your personal situation, the code could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="lines" match="*[starts-with(local-name(),'line')]" use="ItemCode" />

  <xsl:template match="/">
    <Document_Lines>
        <xsl:for-each select="/Root/Head/DocumentLines/*[generate-id()=generate-id(key('lines',ItemCode))]">
            <row>
                <LineNum><xsl:value-of select="LineNum" /></LineNum>
                <ItemCode><xsl:value-of select="ItemCode" /> </ItemCode>
                <Description><xsl:value-of select="Dscription" /></Description>
                <Quantity><xsl:value-of select="sum(key('lines',ItemCode)/Quantity)" /></Quantity>
            </row>
        </xsl:for-each>
    </Document_Lines>
  </xsl:template>

</xsl:stylesheet>
<?xml version="1.0"?>
<Document_Lines>
  <row>
    <LineNum>0</LineNum>
    <ItemCode>A1234</ItemCode>
    <Description>Article1</Description>
    <Quantity>10</Quantity>
  </row>
  <row>
    <LineNum>1</LineNum>
    <ItemCode>B4321</ItemCode>
    <Description>Article2</Description>
    <Quantity>6</Quantity>
  </row>
  <row>
    <LineNum>2</LineNum>
    <ItemCode>A789</ItemCode>
    <Description>Article3</Description>
    <Quantity>8</Quantity>
  </row>
</Document_Lines>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Document_Lines>
        <xsl:for-each-group select="/Root/Head/DocumentLines/*[starts-with(local-name(),'line')]" group-by="ItemCode">
            <row>
                <xsl:copy-of select="LineNum, ItemCode, Dscription" />
                <Quantity><xsl:value-of select="sum(current-group()/Quantity)" /></Quantity>
            </row>
        </xsl:for-each-group>
    </Document_Lines>
  </xsl:template>

</xsl:stylesheet>
Related Posts Related QUESTIONS :
  • Describe repeating XML nodes in W3C XML Schema?
  • Java/XML: Good "Stream-based" Alternative to JAXB?
  • Eclipse complains android:scrollbars and android:fadingEdge do not allow Strings - includes code
  • DTD (or XSD) problem with duplicate name element having different scopes
  • Most efficient way to store list structure in XML
  • What is the correct syntax for XSD to restrict attrributes values in the elements list?
  • Web based XML Editor
  • Looking for a free xsd scheme editor
  • Use XSLT to arrange a list of items in a table
  • XML: Check if anything exists between two nodes
  • Metatool for automatic xml code generation
  • Looking for a library of XSLT to create SVG charts
  • How to decide when to use XSD or DTD?
  • BlackBerry - Extract data from 3rd nesting level of xml
  • XSD schema for recursive XML
  • nokogiri xml unescape
  • How to visualize an XML schema?
  • NSString to NSData Failing in Encoding
  • XmlTextWriter.WriteFullEndElement tags on the same line
  • The case of "id" attribute when signing an element in a XML file
  • Increment a counter in VXML
  • how to build xml schema
  • How define several elements with same name, but different type in xsd:choice element?
  • How can you tell if an XMLlist object in AS3 contains a node?
  • Can XML schema be written to allow xs:attribute of either xs:date or xs:dateTime?
  • how to insert a SAML Assertion to SOAPHeader
  • How to migrate from XslTransform to XslCompiledTransform
  • Is there any better IDOMImplementation other than MSXML?
  • Indesign and XML - how to auto flow into multiple pages with differing styles?
  • XSLT - Add a XML node as header
  • Would like to validate a reference to a container in an XML
  • Dataweave - Trying to write END_DOCUMENT when document has no root
  • How can i export Orbeon form data into xml
  • Using XMLQUERY to retrieve specific XML data
  • Is there a semantical difference between <tag /> and <tag></tag> in XML?
  • Importing xml with nested nodes into Access (and needing to transform the xml into a 'flat' structure with xsl)
  • Why is my modified XML not saving the modified values?
  • How to ensure certain immediate siblings always appear in a specific order?
  • How do you handle complex namespaces in XQuery/BaseX?
  • Removing invalid characters from XML in Python3 with etree
  • Grouping/Paginating items from two different parents
  • contracted/expanded tabular display for all principal xml lines
  • How to establish a custom xml serialization with content negotiation in spring boot 2
  • Overcome 2GB limit on XML output in SQL Server
  • How to add a node to an existing XML document using powershell
  • Why is the attribute also append to the child node?
  • Escaping a Dash in XML Element Name With Powershell
  • What is the use of XML?
  • Create dynamic list/array of string - int pairs based on XML input
  • How to flatten a very complex XML into a new XML containing all nodes at root level
  • Looping and summing values from a different tag values in XSLT
  • XML to csv do the correct xslt
  • XML 'name' attribute, and XML variable type with SelectNodes
  • Remove elements from XML based on complex condition
  • Doctrine Invalid Mapping File Exception when trying to generate entities
  • Is it possible to use icons next to radio buttons in odoo 12
  • View responsible for top navbar in Odoo12
  • Best way to query different xml elements as one column
  • unexpected token while reading xml to string in JSR223 sampler
  • Concatenate XML in SQL stored Procedure
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com