logo
down
shadow

Counting frequency of tags / attributes in XML using XSLT


Counting frequency of tags / attributes in XML using XSLT

By : AlpineLakes
Date : November 18 2020, 03:49 PM
it fixes the issue As an extension to the question that solved here, how we can count number of frequency of each tag and attribute? , Try this:
XSLT:
code :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:key name="kEleName" match="*" use="local-name()"/>
<xsl:key name="kAttribName" match="@*" use="local-name()"/>

<xsl:variable name="var1">
    <xsl:for-each select="/a/*/@*[generate-id() = generate-id(key('kAttribName', name()))]">
        <xsl:value-of select="concat(name(.), ' ', count(key('kAttribName', name(.))))"/>
        <xsl:if test="not(position()=last())">
        <xsl:text>,&#160;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

    <xsl:template match="/*">
       <xsl:apply-templates select="*[generate-id() = generate-id(key('kEleName', name()))]"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:if test="position()=1"><xsl:text>For tags:&#160;</xsl:text></xsl:if>
            <xsl:value-of select="concat(name(.), ' ', count(key('kEleName', name(.))))"/>
            <xsl:if test="following-sibling::*">
                <xsl:text>,&#160;</xsl:text>
            </xsl:if>

            <xsl:if test="position()=last()">
                <xsl:text>&#10;For attributes:&#160;</xsl:text>
            <xsl:value-of select="$var1"/>
            </xsl:if>

    </xsl:template>
</xsl:stylesheet>
<a>
    <apple color="red"/>
    <apple color="green"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</a>
For tags: apple 2, banana 1, sugar 1, cat 1 
For attributes: color 3, taste 1, size 1 


Share : facebook icon twitter icon
XSLT to remove tags/attributes belonging to a namespace

XSLT to remove tags/attributes belonging to a namespace


By : ashar
Date : March 29 2020, 07:55 AM
I hope this helps . I'm trying to use XSLT to remove tags/attributes belonging to a namespace. The difficulty is that tags from differnt namespaces can be embedded in each other. , This transformation:
code :
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns1="http://s1">
 <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=
 "*[not(attribute::ns1:*)
  and
    not(descendant-or-self::ns1:*)
   ]
 |
  @*[not(namespace-uri()='http://s1')]
 "/>
</xsl:stylesheet>
<Collection xmlns="http://s0" xmlns:ns1="http://s1">
    <Identifier Name="CollectionX"
      ns1:GlobalID="{E436833B-B0A6-4E0D-804B-60052B767AE3}"
      ns1:LocalID="{0130C866-7A91-4544-A82B-E0C0F2E3BCB2}"  />
    <Properties>
        <ns1:Collectible>1982</ns1:Collectible>
        <Displayed>Reserved</Displayed>
        <Picture>Reserved.jpeg</Picture>
    </Properties>
    <WeakLinks>
        <Link Type="resource" Language="en-us"/>
    </WeakLinks>
</Collection>
<Collection xmlns="http://s0" xmlns:ns1="http://s1">
   <Identifier ns1:GlobalID="{E436833B-B0A6-4E0D-804B-60052B767AE3}"
   ns1:LocalID="{0130C866-7A91-4544-A82B-E0C0F2E3BCB2}"/>
   <Properties>
      <ns1:Collectible>1982</ns1:Collectible>
   </Properties>
</Collection>
Counting previous Attributes with a specific value in XSLT

Counting previous Attributes with a specific value in XSLT


By : Alexey Kisel
Date : March 29 2020, 07:55 AM
I wish this help you The following seems to me the simplest solution, and gives the wanted output:
code :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="box//*[@*='?']">
  <xsl:value-of select="'&#xa;//', name(), ' = ', 
     count((ancestor::*|preceding::*)/@*[.='?'])"/>
  <xsl:apply-templates/>
</xsl:template>

<xsl:strip-space elements="*"/>

</xsl:stylesheet>
Counting unique XML element attributes using XSLT

Counting unique XML element attributes using XSLT


By : Ramize Attia
Date : March 29 2020, 07:55 AM
will help you I've just started using XSLT and am trying to figure this out. Your help would be greatly appreciated. , A possible Xslt 1.0 solution would be
code :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:key name="kCustomer" match="item" use="@customer_id"/>

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="purchases">
        <xsl:element name="table">
            <xsl:element name="tr">
                <xsl:element name="th">Customer ID</xsl:element>
                <xsl:element name="th">Number of Items Purchased</xsl:element>
                <xsl:for-each select="item[generate-id(.) = generate-id(key('kCustomer', @customer_id))]">
                    <xsl:variable name="total" select="count(/purchases/item[@customer_id=current()/@customer_id]/@id)" />
                    <xsl:element name="tr">
                        <xsl:element name="td">
                            <xsl:value-of select="./@customer_id" />
                        </xsl:element>
                        <xsl:element name="td">
                            <xsl:value-of select="$total" />
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
    <xsl:template match="purchases">
        <table>
            <tr>
                <th>Customer ID</th>
                <th>Number of Items Purchased</th>
            </tr>
            <xsl:for-each select="item[generate-id(.) = generate-id(key('kCustomer', @customer_id))]">
                <xsl:variable name="total" select="count(/purchases/item[@customer_id=current()/@customer_id]/@id)" />
                    <tr>
                        <td>
                            <xsl:value-of select="./@customer_id" />
                        </td>
                        <td>
                            <xsl:value-of select="$total" />
                        </td>
                    </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
XSLT -- Counting attributes within an element

XSLT -- Counting attributes within an element


By : rkomakula
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have XML similar to , You can use a trick like this
code :
<xsl:variable name="descendants"
    select="following-sibling::COLHEAD[@H = 3]
       [generate-id((preceding-sibling::COLHEAD[@H=2])[last()])
         = generate-id(current())]"/>
How to transform attributes into tags using XSLT and avoid duplicates?

How to transform attributes into tags using XSLT and avoid duplicates?


By : agonistes
Date : March 29 2020, 07:55 AM
This might help you You don't say if you are using XSLT 1.0 or XSLT 2.0.
This is a multi-level sort that, for XSLT 1.0, I tell my students is more easily written using the variable-based grouping method.
code :
T:\ftemp>type mxps.xml 
<Accounts>
    <Account Category="001" Region="AAA" Name="dolor" Value="123" Type="A" Rating="1,25"/>
    <Account Category="001" Region="AAA" Name="sit amet" Value="134" Type="A" Rating="1,25"/>
    <Account Category="004" Region="BBB" Name="consetetur" Value="434" Type="A" Rating="1,25"/>
    <Account Category="002" Region="AAA" Name="sadipscing" Value="84" Type="A" Rating="1,25"/>
    <Account Category="007" Region="ZZZ" Name="elitr" Value="33" Type="A" Rating="1,25"/>
    <Account Category="004" Region="CCC" Name="aliquyam" Value="6" Type="A" Rating="1,25"/>
    <Account Category="001" Region="BBB" Name="ipsum" Value="34" Type="A" Rating="1,25"/>
    <Account Category="003" Region="ZZZ" Name="lorem" Value="75" Type="A" Rating="2.87"/>
</Accounts>
T:\ftemp>call xslt mxps.xml mxps.xsl 
<?xml version="1.0" encoding="utf-8"?>
<Accounts>
   <Category name="001">
      <Region name="AAA">
         <Account Name="dolor" Value="123" Type="A" Rating="1,25"/>
         <Account Name="sit amet" Value="134" Type="A" Rating="1,25"/>
      </Region>
      <Region name="BBB">
         <Account Name="ipsum" Value="34" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="002">
      <Region name="AAA">
         <Account Name="sadipscing" Value="84" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="003">
      <Region name="ZZZ">
         <Account Name="lorem" Value="75" Type="A" Rating="2.87"/>
      </Region>
   </Category>
   <Category name="004">
      <Region name="BBB">
         <Account Name="consetetur" Value="434" Type="A" Rating="1,25"/>
      </Region>
      <Region name="CCC">
         <Account Name="aliquyam" Value="6" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="007">
      <Region name="ZZZ">
         <Account Name="elitr" Value="33" Type="A" Rating="1,25"/>
      </Region>
   </Category>
</Accounts>
T:\ftemp>type mxps.xsl 
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="Accounts">
  <Accounts>
    <xsl:variable name="accounts" select="Account"/>
    <xsl:for-each select="$accounts">
      <xsl:sort select="@Category"/>
      <xsl:if test="generate-id(.)=
                    generate-id($accounts[@Category=current()/@Category][1])">
        <Category name="{@Category}">
          <xsl:variable name="categories"
                        select="$accounts[@Category=current()/@Category]"/>
          <xsl:for-each select="$categories">
            <xsl:sort select="@Region"/>
            <xsl:if test="generate-id(.)=
                       generate-id($categories[@Region=current()/@Region][1])">
              <Region name="{@Region}">
                <xsl:for-each select="$categories[@Region=current()/@Region]">
                  <xsl:sort select="@Name"/>
                  <Account>
                    <xsl:copy-of select="@Name"/>
                    <xsl:copy-of select="@Value"/>
                    <xsl:copy-of select="@Type"/>
                    <xsl:copy-of select="@Rating"/>
                  </Account>
                </xsl:for-each>
              </Region>
            </xsl:if>
          </xsl:for-each>
        </Category>
      </xsl:if>
    </xsl:for-each>
  </Accounts>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>call xslt2 mxps.xml mxps2.xsl 
<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
   <Category name="001">
      <Region name="AAA">
         <Account Name="dolor" Value="123" Type="A" Rating="1,25"/>
         <Account Name="sit amet" Value="134" Type="A" Rating="1,25"/>
      </Region>
      <Region name="BBB">
         <Account Name="ipsum" Value="34" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="002">
      <Region name="AAA">
         <Account Name="sadipscing" Value="84" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="003">
      <Region name="ZZZ">
         <Account Name="lorem" Value="75" Type="A" Rating="2.87"/>
      </Region>
   </Category>
   <Category name="004">
      <Region name="BBB">
         <Account Name="consetetur" Value="434" Type="A" Rating="1,25"/>
      </Region>
      <Region name="CCC">
         <Account Name="aliquyam" Value="6" Type="A" Rating="1,25"/>
      </Region>
   </Category>
   <Category name="007">
      <Region name="ZZZ">
         <Account Name="elitr" Value="33" Type="A" Rating="1,25"/>
      </Region>
   </Category>
</Accounts>

T:\ftemp>type mxps2.xsl 
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="Accounts">
  <Accounts>
    <xsl:for-each-group select="Account" group-by="@Category">
      <xsl:sort select="@Category"/>
        <Category name="{@Category}">
          <xsl:for-each-group select="current-group()" group-by="@Region">
            <xsl:sort select="@Region"/>
              <Region name="{@Region}">
                <xsl:for-each select="current-group()">
                  <xsl:sort select="@Name"/>
                  <Account>
                    <xsl:copy-of select="@Name,@Value,@Type,@Rating"/>
                  </Account>
                </xsl:for-each>
              </Region>
          </xsl:for-each-group>
        </Category>
    </xsl:for-each-group>
  </Accounts>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>rem Done! 
Related Posts Related Posts :
  • How can I debug a corrupt docx file?
  • Validating XML with standard XSD schema in Visual Studio 2017
  • flex: display number of children in datagrid column
  • Default namespace in XML file doesn't work with XSD, why?
  • Spring 3.1 : Cannot find the declaration of element 'beans'
  • Merging similar attributes with XSLT
  • WSO2 ESB: XML response in WSO2 REST API call not parsed
  • Add image id to image path based on condition
  • How to test data of an XML file
  • how can i solve "No grammar constraints (DTD or XML Schema) referenced in the document." WARNING
  • Why does XML not display in browser but correctly does in XML Notepad 2007 (Microsoft)?
  • How to output based on the input conditions
  • XML to Fixed width text file formatted details
  • Use parameter value as in select in xslt
  • Counting the number of times an xslt transform is done
  • Trouble using an environment variable's value in VBS
  • HTTP response containing XML book-ended by unexpected characters
  • Trying to retrieve just one node value in xslt
  • Select name of alias of xmlns atribute by xpath
  • Get and set text in a xml field
  • Need help to create an XSLT
  • Update nodes via merge without changing node order?
  • Flat to Hierarchical XSLT Transform
  • XSL Selecting A Nested Field (From Shopify XML Output)
  • How do I add a semi colon between text fragments but not at the end of text fragments?
  • Parse XSL Date Format
  • Network protocol structure definitions in machine readable form (xml,json,other?)?
  • invalid byte '?' at position 2 of 2-byte sequence validation/parse error
  • Inserting a node into XML
  • Replace before and after string in vim
  • XML/XSL Table won't populate
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org