问:
Trouble creating a nested xml from flat xml
I am trying to create a nested xml from a flat XML using an XSLT however I have found that it only creates one nest and ignores the rest of the records in the source XML.

My XML input looks like this:

```<code><?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Data -->
<table name="ecatalogue">
  <!-- Row 1 -->
  <tuple>
    <atom name="irn">2470</atom>
    <atom name="EADUnitID">da.01</atom>
    <atom name="EADUnitTitle">Some title</atom>
    <tuple name="AssParentObjectRef" />
  </tuple>
    <!-- Row 2 -->
  <tuple>
    <atom name="irn">5416</atom>
    <atom name="EADUnitID">da.01.01</atom>
    <atom name="EADUnitTitle">Child of Some title</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Some Title</atom>
    <atom name="irn">2470</atom>
    </tuple>
  </tuple>
    <!-- Row 3 -->
  <tuple>
    <atom name="irn">6</atom>
    <atom name="EADUnitID">da.01.02</atom>
    <atom name="EADUnitTitle">Child of Some title 2</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Some Title</atom>
    <atom name="irn">2470</atom>
    </tuple>
  </tuple>
    <!-- Row 4 -->
  <tuple>
    <atom name="irn">8</atom>
    <atom name="EADUnitID">da.01.02.01</atom>
    <atom name="EADUnitTitle">3rd Generation</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Child of Some Title 2</atom>
    <atom name="irn">6</atom>
    </tuple>
  </tuple>
    <!-- Row 5 -->
  <tuple>
    <atom name="irn">1130</atom>
    <atom name="EADUnitID">da.02</atom>
    <atom name="EADUnitTitle">Another title</atom>
    <tuple name="AssParentObjectRef" />
  </tuple>
    <!-- Row 6 -->
  <tuple>
    <atom name="irn">54</atom>
    <atom name="EADUnitID">da.02.01</atom>
    <atom name="EADUnitTitle">Child of Another title</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Another Title</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>
    <!-- Row 7 -->
  <tuple>
    <atom name="irn">16</atom>
    <atom name="EADUnitID">da.02.02</atom>
    <atom name="EADUnitTitle">Child of Another Title 2</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Another Title</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>
    <!-- Row 8 -->
  <tuple>
    <atom name="irn">22</atom>
    <atom name="EADUnitID">da.02.02.01</atom>
    <atom name="EADUnitTitle">3rd Generation</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Child of Another Title 2</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>
</table>
</code>```

The XSLT should identify the top level record and then add the children. For the top record it should duplicate its irn and EADUnitTitle as TopID and TopTitle respectively. For each child it should include the immediate ParentID and ParentTitle as well as the TopID and TopTitle. The output should look like:

```<code><?xml version="1.0" encoding="UTF-8"?>
<table name="ecatalogue">
   <collection>
      <tuple>
         <atom name="irn">2470</atom>
         <atom name="EADUnitID">da.01</atom>
         <atom name="EADUnitTitle">Some title</atom>
         <atom name="TopTitle">Some title</atom>
         <atom name="TopID">2470</atom>
         <tuple name="children">
            <tuple>
               <atom name="irn">5416</atom>
               <atom name="EADUnitID">da.01.01</atom>
               <atom name="EADUnitTitle">Child of Some title</atom>
               <atom name="ParentTitle">Some title</atom>
               <atom name="ParentID">2470</atom>
               <atom name="TopTitle">Some title</atom>
               <atom name="TopID">2470</atom>
            </tuple>
            <tuple>
                <atom name="irn">6</atom>
               <atom name="EADUnitID">da.01.02</atom>
               <atom name="EADUnitTitle">Child of Some title 2</atom>
               <atom name="ParentTitle">Some title</atom>
               <atom name="ParentID">2470</atom>
               <atom name="TopTitle">Some title</atom>
               <atom name="TopID">2470</atom>
               <tuple name="children">
                  <tuple>
                    <atom name="irn">8</atom>
                    <atom name="EADUnitID">da.01.02.01</atom>
                    <atom name="EADUnitTitle">3rd Generation</atom>
                    <atom name="ParentTitle">Child of Some title 2</atom>
                    <atom name="ParentID">6</atom>
                    <atom name="TopTitle">Some title</atom>
                    <atom name="TopID">2470</atom>
                  </tuple>
               </tuple>
            </tuple>
         </tuple>
      </tuple>
   </collection>
   <collection>
      <tuple>
         <atom name="irn">1130</atom>
         <atom name="EADUnitID">da.02</atom>
         <atom name="EADUnitTitle">Another title</atom>
         <atom name="TopTitle">Another title</atom>
         <atom name="TopID">1130</atom>
         <tuple name="children">
            <tuple>
               <atom name="irn">54</atom>
               <atom name="EADUnitID">da.02.01</atom>
               <atom name="EADUnitTitle">Child of Another title</atom>
               <atom name="ParentTitle">Another title</atom>
               <atom name="ParentID">1130</atom>
               <atom name="TopTitle">Another title</atom>
               <atom name="TopID">1130</atom>
            </tuple>
            <tuple>
                <atom name="irn">16</atom>
               <atom name="EADUnitID">da.02.02</atom>
               <atom name="EADUnitTitle">Child of Another title 2</atom>
               <atom name="ParentTitle">Another title</atom>
               <atom name="ParentID">1130</atom>
               <atom name="TopTitle">Another title</atom>
               <atom name="TopID">1130</atom>
               <tuple name="children">
                  <tuple>
                    <atom name="irn">22</atom>
                    <atom name="EADUnitID">da.02.02.01</atom>
                    <atom name="EADUnitTitle">3rd Generation</atom>
                    <atom name="ParentTitle">Child of Another title 2</atom>
                    <atom name="ParentID">16</atom>
                    <atom name="TopTitle">Another title</atom>
                    <atom name="TopID">1130</atom>
                  </tuple>
               </tuple>
            </tuple>
         </tuple>
      </tuple>

....

   </collection>
</table>
</code>```

The XSLT I have is:

```<code><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:key name="child" match="tuple" use="tuple[@name='AssParentObjectRef']/atom[@name='irn']" />

<xsl:template match="/table">
    <table name="ecatalogue">
        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>
    </table>
</xsl:template>

<xsl:template match="tuple">
    <tuple>
        <xsl:copy-of select="atom"/>
        <xsl:if test="key('child', atom[@name='irn'])">
            <tuple name="children">
                <xsl:apply-templates select="key('child', atom[@name='irn'])"/>
             </tuple>
        </xsl:if>
    </tuple>
</xsl:template>

</xsl:stylesheet>
</code>```

And whilst this will group the records, the output is just one of these collections. So from a file of 3524 records, I get one collection of 24 records.

I've experimented with the XSLT replacing:

```<code><xsl:template match="/table">
    <table name="ecatalogue">
        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>
    </table>
</xsl:template>
</code>```

With:

```<code><xsl:template match="node()|@*">
   <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>
</code>```

And whilst this returns all the nested structures, it also duplicates the records within the nests so they become collections in themselves.

Any ideas on where I'm going wrong?

EDIT 06/06/17

When I use:

```<code><xsl:template match="node()|@*">
       <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>
</code>```

I get duplicates (note: the 'id' in the below example is added for illustration):

```<code> <record id='1'>
   <children>
        <record id='2'>
            <children>
                <record id='3'>
                    <children>
                        <record id='4'></record>
                    </children>
                </record>
            </children>
        </record>
    </children>
</record>

<record id='2'>
        <children>
            <record id='3'>
                <children>
                    <record id='4'></record>
                </children>
            </record>
        </children>
</record>

<record id='3'>
    <children>
        <record id='4'></record>
    </children>
</record>

<record id='2'></record>
<record id='3'></record>
<record id='4'></record>
</code>```

Is there anyway to remove the duplicates so I'm just left with the nested records?

EDIT - Problem Tuples

```<code> <!-- Row 3378 -->
  <tuple>
    <atom name="irn">115024</atom>
    <atom name="ObjectType">Archives</atom>
    <atom name="EADLevelAttribute">Series</atom>
    <atom name="EADUnitID">D42.PL.05</atom>
    <atom name="EADUnitTitle">Correspondence and Company Administration: Box Files</atom>
    <atom name="EADScopeAndContent">Box files of Port Line official company correspondence and administrative papers. These papers were collected towards historical research and include correspondence from earlier periods c.1890 although the bulk of the papers relate to the two periods 1937-1939 and 1949-1951.</atom>
    <atom name="EADBiographyOrHistory"></atom>
    <tuple name="AssParentObjectRef">
    </tuple>
    <atom name="EADArrangement">The papers in this series have been retained in the original order as stored by Port Line Ltd. The contents of each box file are listed as a typescript paper and have been listed in this catalogue. Box file titles have been listed in the title field of each item in this series.</atom>
    <atom name="EADUnitDate">1890-1952</atom>
    <table name="EADExtent_tab">
      <tuple>
        <atom name="EADExtent">7 boxes.</atom>
      </tuple>
    </table>
    <atom name="EADAccruals"></atom>
    <atom name="EADOtherFindingAid"></atom>
    <atom name="EADRelatedMaterial"></atom>
    <tuple name="EADAcquisitionInformationRef">
    </tuple>
    <atom name="EADAppraisalInformation"></atom>
    <atom name="EADSeparatedMaterial"></atom>
    <atom name="EADTitleProper"></atom>
    <atom name="EADPublicationStatement"></atom>
    <atom name="EADCustodialHistory"></atom>
    <atom name="EADSource"></atom>
    <atom name="EADNote"></atom>
    <atom name="EADAccessRestrictions">Some items in this series are closed access.</atom>
    <atom name="EADUseRestrictions"></atom>
  </tuple>

  <!-- Row 3379 -->
  <tuple>
    <atom name="irn">115025</atom>
    <atom name="ObjectType">Archives</atom>
    <atom name="EADLevelAttribute">Item</atom>
    <atom name="EADUnitID">D42.PL.05.01</atom>
    <atom name="EADUnitTitle">File: Australian Homeward Trade</atom>
    <atom name="EADScopeAndContent">Various papers relating to Australian Homeward Trade and includes the following:For proof copies of the Australian Homeward Agreement see D42/PL5/6.</atom>
    <atom name="EADBiographyOrHistory"></atom>
    <tuple name="AssParentObjectRef">
      <atom name="EADUnitTitle">Correspondence and Company Administration: Box Files</atom>
      <atom name="irn">115024</atom>
    </tuple>
    <atom name="EADArrangement"></atom>
    <atom name="EADUnitDate">1920-1936</atom>
    <table name="EADExtent_tab">
      <tuple>
        <atom name="EADExtent">1 file.</atom>
      </tuple>
    </table>
    <atom name="EADAccruals"></atom>
    <atom name="EADOtherFindingAid"></atom>
    <atom name="EADRelatedMaterial"></atom>
    <tuple name="EADAcquisitionInformationRef">
    </tuple>
    <atom name="EADAppraisalInformation"></atom>
    <atom name="EADSeparatedMaterial"></atom>
    <atom name="EADTitleProper"></atom>
    <atom name="EADPublicationStatement"></atom>
    <atom name="EADCustodialHistory"></atom>
    <atom name="EADSource"></atom>
    <atom name="EADNote"></atom>
    <atom name="EADAccessRestrictions"></atom>
    <atom name="EADUseRestrictions"></atom>
  </tuple>
</code>```

答:
This is a bit long; I try to address everything relevant that caught my attention.

The existing templates

First, let's break down what's going on with your own XSL code.

```<code><xsl:template match="/table">
    <table name="ecatalogue">
        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>
    </table>
</xsl:template>
</code>```

You have <collection> here in the template that matches /table.  Since there is only one /table match, you will only ever have one <collection> in the output.

Also, your selection of top-level tuples (tuple elements that do not refer to a parent object) could be simplified.  Instead of:

```<code>select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"
</code>```

you could more simply say:

```<code>select="tuple[not(tuple/*)]"
</code>```

since we can tell from your sample that top-level tuples only ever contain an empty self-closing <tuple> tag.

The next bit:

```<code><xsl:key name="child" match="tuple" use="tuple[@name='AssParentObjectRef']/atom[@name='irn']" />
</code>```

So we have a key matching on tuples and using the ID of the parent tuple.

```<code><xsl:template match="tuple">
    <tuple>
        <xsl:copy-of select="atom"/>

        <!-- If this `tuple` is a parent (i.e. if it's included in
             the list of parent IDs in the key), then we add a
             wrapper for the children and process the children.  -->
        <xsl:if test="key('child', atom[@name='irn'])">
            <tuple name="children">
                <!-- Now we apply templates to the `tuple`s
                     in the key -->
                <xsl:apply-templates select="key('child', atom[@name='irn'])"/>
            </tuple>
        </xsl:if>
    </tuple>
</xsl:template>
</code>```

This mostly works.  Comparing the output from this against your sample desired output, the bits you're missing are the <collection> wrapper tag (explained above), and the parent and top-level ancestor titles and IDs (for which you don't have any XSL code).

You state,

"And whilst this will group the records, the output is just one of these collections. So from a file of 3524 records, I get one collection of 24 records."

I can hypothesize that the XML structure of the remainder of your actual input might differ from what your XSL is targeting.  But without seeing your actual input, I cannot tell why this might be the case.

Your edit

You describe adding in the following template:

```<code><xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
</code>```

This is the "identity" template, so named because it simply copies over elements identically.  The duplication you're seeing is expected: the XSLT processor goes through the flat set of tuples in the input file and copies those over, and then also processes some of these within the context of the template specifically matching tuple, which applies templates to the tuples that matched the key (the nested children).

Alternative approach

The table template isn't too different:

```<code><xsl:template match="table">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <!-- Target only parent-level `tuple`s.  This excludes
            child-level tuples, helping to prevent duplicates.-->
        <xsl:apply-templates select="tuple[not(tuple/*)]" mode="top"/>
    </xsl:copy>
</xsl:template>
</code>```

Notably, we don't do anything with <collection> here -- we want to wrap each top-level tuple (and its children) in a <collection>, so we need to add that <collection> at the tuple level.

```<code><!-- Add the `collection` wrapper only to top-level tuples -->
<xsl:template match="tuple" mode="top">
    <collection>
        <!-- Pass on this tuple to the main `tuple` template -->
        <xsl:apply-templates select="."/>
    </collection>
</xsl:template>
</code>```

I match the tuple, but using a special mode -- we only want to add the <collection> wrapper for top-level tuples.  I then have a separate template that handles all tuples:

```<code><!-- This is the main template for processing `tuple` elements.
    Most of the changes needed are common to all `tuple`s, so it
    makes sense to keep all the logic in one place. -->
<xsl:template match="tuple">
    <tuple>
        <!-- Copy each existing `atom` child -->
        <xsl:copy-of select="atom"/>
        <!-- Add in metadata about parent and top-level ancestor titles and IDs -->
            <xsl:choose>
                <!-- If this is a top-level item, just use its own values -->
                <xsl:when test="not(tuple/*)">
                    <atom name="TopTitle"><xsl:value-of select="atom[@name='EADUnitTitle']"/></atom>
                    <atom name="TopID"><xsl:value-of select="atom[@name='irn']"/></atom>
                </xsl:when>
                <!-- If this is a descendant, we need to find its parent and its top-level ancestor -->
                <xsl:when test="tuple/*">
                    <atom name="ParentTitle"><xsl:value-of select="tuple/atom[@name='EADUnitTitle']"/></atom>
                    <atom name="ParentID"><xsl:value-of select="tuple/atom[@name='irn']"/></atom>
                <!-- For convenience, grab the top-level ancestor `tuple` and stuff it in a variable.
                    This is vaguely annalogous to your use of `key`. -->

                <!-- Finding the top-level `tuple` is complicated by the fact that the ID values in
                    `<atom name="irn">` do not have a standardized format, other than that the whole
                    strings appear to consist of atomic values separated by single periods, with
                    descendant `irn` values appending to the precedent values.  Examples:
                      Top:        `da.04`
                      Descendant: `da.04.11.02`
                      Top:        `D42.PL.05`
                      Descendant: `D42.PL.05.01`
                    So chunking the ID values is a problematic approach, since we don't know how many
                    chunks comprise the initial non-numeric portion: `da`, or `D42.PL`, or ... ???.
                    Top-level elements *do* also have empty `<tuple name="AssParentObjectRef">` elements.
                    So we _can_ find all the top-level elements, and then look in those for the one that
                    has an `irn` value that matches the start of the `irn` value of this current `tuple`. -->
                <xsl:variable name="top" select="/table/tuple[tuple[@name='AssParentObjectRef'][not(*)]]
                    ['The above statement grabs all the `tuple`s that have an empty `tuple[@name=`AssParentObjectRef``.
                      The below statement then goes through all those `tuple`s to find the ones where the `irn`
                      values match the start of the `irn` value of the current `tuple`.']
                    [starts-with(current()/atom[@name='EADUnitID'], atom[@name='EADUnitID'])]"/>
                    <!-- Now we can reference that variable to get the top-level ancestor values -->
                    <atom name="TopTitle"><xsl:value-of select="$top/atom[@name='EADUnitTitle']"/></atom>
                    <atom name="TopID"><xsl:value-of select="$top/atom[@name='irn']"/></atom>
                </xsl:when>
            </xsl:choose>
        <!-- Process any children of this tuple, based on `irn` values.
            Basically, we look for any other `tuple`s in the `table`
            that point to this current `tuple`'s `irn` value. -->
        <xsl:if test="/table/tuple[tuple/atom[@name='irn'] = current()/atom[@name='irn']]">
            <tuple name="children">
                <xsl:apply-templates select="/table/tuple[tuple/atom[@name='irn'] = current()/atom[@name='irn']]"></xsl:apply-templates>
            </tuple>
        </xsl:if>
    </tuple>
</xsl:template>
</code>```

I can't speak to your full dataset of 3524 records, but running the above against your sample input XML, my output is identical to your desired output (except for the one bug in your sample input XML about a referenced irn value, mentioned in a comment on your initial post).

Suggestions about your desired output XML

As a data format, your desired output XML has a number of aspects that strike me as a bit odd.

The <collection> wrapper

This seems superfluous; it's easy enough to see if a given tuple 1) is top-level, and 2) has any children.

The Parent and Top titles and IDs

These similarly seem superfluous.  So long as your data is structured hierarchically, all of this becomes clear without any need to specifically include it.  Including this metadata just duplicates already-available information.

The <tuple name="children"> wrapper

This could also be omitted without any loss of information.  The simple presence of one tuple element nested within another is evidence enough of children.

I don't know if you have any control over, or influence on, the design of the output XML file format, but if you do, I'd suggest a shorter and more streamlined structure, such as the following:

```<code><table name="ecatalogue">
    <tuple>
        <atom name="irn">2470</atom>
        <atom name="EADUnitID">da.01</atom>
        <atom name="EADUnitTitle">Some title</atom>
        <tuple>
            <atom name="irn">5416</atom>
            <atom name="EADUnitID">da.01.01</atom>
            <atom name="EADUnitTitle">Child of Some title</atom>
        </tuple>
        <tuple>
            <atom name="irn">6</atom>
            <atom name="EADUnitID">da.01.02</atom>
            <atom name="EADUnitTitle">Child of Some title 2</atom>
            <tuple>
                <atom name="irn">8</atom>
                <atom name="EADUnitID">da.01.02.01</atom>
                <atom name="EADUnitTitle">3rd Generation</atom>
            </tuple>
        </tuple>
    </tuple>
</table>
</code>```

In this structure, a tuple can only contain either atoms or other tuples.  We can identify a collection by simply finding any top-level tuple that contains other tuples.  We can identify children by simply finding any tuple that has a tuple parent.  We can find top-level and parent-level titles and IDs by simply picking a tuple and looking further up the element tree.

This structure is simpler, avoids data duplication, and is arguably clearer and easier to process.  That said, you know your needs!  Do what works for you.  :)

Please have a look through the code and comments, and let me know if you have any lingering questions.

Update 2017-06-15: Problem tuples

I added these to your previous sample input XML, and tried applying my previous XSL code.  In reviewing the problem tuples, two things occurred to me:

My previous code makes use of the tokenize function, which is only available in XSL 2.0 and newer.  Your post's tags did not specify XSL 1.0, and I failed to notice that you'd specified this in your sample XSL header.

I've reworked the XSL code above (the template that matches on tuple) to rely only on XSL 1.0 features.

The EADUnitID values in your initial sample input XML are not representative, so any attempt to code specifically against that sample is bound to fail when applied to your fuller, unseen input XML.

Your initial sample only includes EADUnitID values of the format da.XX, where XX is digits, and the .XX pattern can repeat.  I had made some assumptions about chunking this string and comparing the pieces.

However, your problem tuples have EADUnitID values of a very different format, which appears to be DXX.PL.XX, where again XX is digits and the .XX pattern on the end can repeat.  This means that relying on conformity of the chunks between periods is not a safe approach.

I've reworked the XSL code to instead match the entire front end of the irn string, which appears to work reliably.

Have a look at the code, and the comments, and let me know if anything remains unclear or non-functional.

Body:
I am trying to create a nested xml from a flat XML using an XSLT however I have found that it only creates one nest and ignores the rest of the records in the source XML.

My XML input looks like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>

  <tuple>
    <atom name="irn">2470</atom>
    <atom name="EADUnitID">da.01</atom>
    <atom name="EADUnitTitle">Some title</atom>
    <tuple name="AssParentObjectRef" />
  </tuple>

  <tuple>
    <atom name="irn">5416</atom>
    <atom name="EADUnitID">da.01.01</atom>
    <atom name="EADUnitTitle">Child of Some title</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Some Title</atom>
    <atom name="irn">2470</atom>
    </tuple>
  </tuple>

  <tuple>
    <atom name="irn">6</atom>
    <atom name="EADUnitID">da.01.02</atom>
    <atom name="EADUnitTitle">Child of Some title 2</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Some Title</atom>
    <atom name="irn">2470</atom>
    </tuple>
  </tuple>

  <tuple>
    <atom name="irn">8</atom>
    <atom name="EADUnitID">da.01.02.01</atom>
    <atom name="EADUnitTitle">3rd Generation</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Child of Some Title 2</atom>
    <atom name="irn">6</atom>
    </tuple>
  </tuple>

  <tuple>
    <atom name="irn">1130</atom>
    <atom name="EADUnitID">da.02</atom>
    <atom name="EADUnitTitle">Another title</atom>
    <tuple name="AssParentObjectRef" />
  </tuple>

  <tuple>
    <atom name="irn">54</atom>
    <atom name="EADUnitID">da.02.01</atom>
    <atom name="EADUnitTitle">Child of Another title</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Another Title</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>

  <tuple>
    <atom name="irn">16</atom>
    <atom name="EADUnitID">da.02.02</atom>
    <atom name="EADUnitTitle">Child of Another Title 2</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Another Title</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>

  <tuple>
    <atom name="irn">22</atom>
    <atom name="EADUnitID">da.02.02.01</atom>
    <atom name="EADUnitTitle">3rd Generation</atom>
    <tuple name="AssParentObjectRef">
    <atom name="EADUnitTitle">Child of Another Title 2</atom>
    <atom name="irn">1130</atom>
    </tuple>
  </tuple>

The XSLT should identify the top level record and then add the children. For the top record it should duplicate its irn and EADUnitTitle as TopID and TopTitle respectively. For each child it should include the immediate ParentID and ParentTitle as well as the TopID and TopTitle. The output should look like:

<?xml version="1.0" encoding="UTF-8"?>

   <collection>
      <tuple>
         <atom name="irn">2470</atom>
         <atom name="EADUnitID">da.01</atom>
         <atom name="EADUnitTitle">Some title</atom>
         <atom name="TopTitle">Some title</atom>
         <atom name="TopID">2470</atom>
         <tuple name="children">
            <tuple>
               <atom name="irn">5416</atom>
               <atom name="EADUnitID">da.01.01</atom>
               <atom name="EADUnitTitle">Child of Some title</atom>
               <atom name="ParentTitle">Some title</atom>
               <atom name="ParentID">2470</atom>
               <atom name="TopTitle">Some title</atom>
               <atom name="TopID">2470</atom>
            </tuple>
            <tuple>
                <atom name="irn">6</atom>
               <atom name="EADUnitID">da.01.02</atom>
               <atom name="EADUnitTitle">Child of Some title 2</atom>
               <atom name="ParentTitle">Some title</atom>
               <atom name="ParentID">2470</atom>
               <atom name="TopTitle">Some title</atom>
               <atom name="TopID">2470</atom>
               <tuple name="children">
                  <tuple>
                    <atom name="irn">8</atom>
                    <atom name="EADUnitID">da.01.02.01</atom>
                    <atom name="EADUnitTitle">3rd Generation</atom>
                    <atom name="ParentTitle">Child of Some title 2</atom>
                    <atom name="ParentID">6</atom>
                    <atom name="TopTitle">Some title</atom>
                    <atom name="TopID">2470</atom>
                  </tuple>
               </tuple>
            </tuple>
         </tuple>
      </tuple>
   </collection>
   <collection>
      <tuple>
         <atom name="irn">1130</atom>
         <atom name="EADUnitID">da.02</atom>
         <atom name="EADUnitTitle">Another title</atom>
         <atom name="TopTitle">Another title</atom>
         <atom name="TopID">1130</atom>
         <tuple name="children">
            <tuple>
               <atom name="irn">54</atom>
               <atom name="EADUnitID">da.02.01</atom>
               <atom name="EADUnitTitle">Child of Another title</atom>
               <atom name="ParentTitle">Another title</atom>
               <atom name="ParentID">1130</atom>
               <atom name="TopTitle">Another title</atom>
               <atom name="TopID">1130</atom>
            </tuple>
            <tuple>
                <atom name="irn">16</atom>
               <atom name="EADUnitID">da.02.02</atom>
               <atom name="EADUnitTitle">Child of Another title 2</atom>
               <atom name="ParentTitle">Another title</atom>
               <atom name="ParentID">1130</atom>
               <atom name="TopTitle">Another title</atom>
               <atom name="TopID">1130</atom>
               <tuple name="children">
                  <tuple>
                    <atom name="irn">22</atom>
                    <atom name="EADUnitID">da.02.02.01</atom>
                    <atom name="EADUnitTitle">3rd Generation</atom>
                    <atom name="ParentTitle">Child of Another title 2</atom>
                    <atom name="ParentID">16</atom>
                    <atom name="TopTitle">Another title</atom>
                    <atom name="TopID">1130</atom>
                  </tuple>
               </tuple>
            </tuple>
         </tuple>
      </tuple>

....

   </collection>

The XSLT I have 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:key name="child" match="tuple" use="tuple[@name='AssParentObjectRef']/atom[@name='irn']" />

<xsl:template match="/table">

        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>

</xsl:template>

<xsl:template match="tuple">
    <tuple>
        <xsl:copy-of select="atom"/>
        <xsl:if test="key('child', atom[@name='irn'])">
            <tuple name="children">
                <xsl:apply-templates select="key('child', atom[@name='irn'])"/>
             </tuple>
        </xsl:if>
    </tuple>
</xsl:template>

</xsl:stylesheet>

And whilst this will group the records, the output is just one of these collections. So from a file of 3524 records, I get one collection of 24 records.

I've experimented with the XSLT replacing:

<xsl:template match="/table">

        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>

</xsl:template>

With:

<xsl:template match="node()|@*">
   <xsl:copy>
     <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>

And whilst this returns all the nested structures, it also duplicates the records within the nests so they become collections in themselves.

Any ideas on where I'm going wrong?

EDIT 06/06/17

When I use:

<xsl:template match="node()|@*">
       <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
    </xsl:template>

I get duplicates (note: the 'id' in the below example is added for illustration):

 <record id='1'>
   <children>
        <record id='2'>
            <children>
                <record id='3'>
                    <children>
                        <record id='4'></record>
                    </children>
                </record>
            </children>
        </record>
    </children>
</record>

<record id='2'>
        <children>
            <record id='3'>
                <children>
                    <record id='4'></record>
                </children>
            </record>
        </children>
</record>

<record id='3'>
    <children>
        <record id='4'></record>
    </children>
</record>

<record id='2'></record>
<record id='3'></record>
<record id='4'></record>

Is there anyway to remove the duplicates so I'm just left with the nested records?

EDIT - Problem Tuples

  <tuple>
    <atom name="irn">115024</atom>
    <atom name="ObjectType">Archives</atom>
    <atom name="EADLevelAttribute">Series</atom>
    <atom name="EADUnitID">D42.PL.05</atom>
    <atom name="EADUnitTitle">Correspondence and Company Administration: Box Files</atom>
    <atom name="EADScopeAndContent">Box files of Port Line official company correspondence and administrative papers. These papers were collected towards historical research and include correspondence from earlier periods c.1890 although the bulk of the papers relate to the two periods 1937-1939 and 1949-1951.</atom>
    <atom name="EADBiographyOrHistory"></atom>
    <tuple name="AssParentObjectRef">
    </tuple>
    <atom name="EADArrangement">The papers in this series have been retained in the original order as stored by Port Line Ltd. The contents of each box file are listed as a typescript paper and have been listed in this catalogue. Box file titles have been listed in the title field of each item in this series.</atom>
    <atom name="EADUnitDate">1890-1952</atom>

      <tuple>
        <atom name="EADExtent">7 boxes.</atom>
      </tuple>

    <atom name="EADAccruals"></atom>
    <atom name="EADOtherFindingAid"></atom>
    <atom name="EADRelatedMaterial"></atom>
    <tuple name="EADAcquisitionInformationRef">
    </tuple>
    <atom name="EADAppraisalInformation"></atom>
    <atom name="EADSeparatedMaterial"></atom>
    <atom name="EADTitleProper"></atom>
    <atom name="EADPublicationStatement"></atom>
    <atom name="EADCustodialHistory"></atom>
    <atom name="EADSource"></atom>
    <atom name="EADNote"></atom>
    <atom name="EADAccessRestrictions">Some items in this series are closed access.</atom>
    <atom name="EADUseRestrictions"></atom>
  </tuple>

  <tuple>
    <atom name="irn">115025</atom>
    <atom name="ObjectType">Archives</atom>
    <atom name="EADLevelAttribute">Item</atom>
    <atom name="EADUnitID">D42.PL.05.01</atom>
    <atom name="EADUnitTitle">File: Australian Homeward Trade</atom>
    <atom name="EADScopeAndContent">Various papers relating to Australian Homeward Trade and includes the following:For proof copies of the Australian Homeward Agreement see D42/PL5/6.</atom>
    <atom name="EADBiographyOrHistory"></atom>
    <tuple name="AssParentObjectRef">
      <atom name="EADUnitTitle">Correspondence and Company Administration: Box Files</atom>
      <atom name="irn">115024</atom>
    </tuple>
    <atom name="EADArrangement"></atom>
    <atom name="EADUnitDate">1920-1936</atom>

      <tuple>
        <atom name="EADExtent">1 file.</atom>
      </tuple>

    <atom name="EADAccruals"></atom>
    <atom name="EADOtherFindingAid"></atom>
    <atom name="EADRelatedMaterial"></atom>
    <tuple name="EADAcquisitionInformationRef">
    </tuple>
    <atom name="EADAppraisalInformation"></atom>
    <atom name="EADSeparatedMaterial"></atom>
    <atom name="EADTitleProper"></atom>
    <atom name="EADPublicationStatement"></atom>
    <atom name="EADCustodialHistory"></atom>
    <atom name="EADSource"></atom>
    <atom name="EADNote"></atom>
    <atom name="EADAccessRestrictions"></atom>
    <atom name="EADUseRestrictions"></atom>
  </tuple>

Body:
This is a bit long; I try to address everything relevant that caught my attention.

The existing templates

First, let's break down what's going on with your own XSL code.

<xsl:template match="/table">
    <table name="ecatalogue">
        <collection>
            <xsl:apply-templates select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"/>
        </collection>
    </table>
</xsl:template>

You have <collection> here in the template that matches /table.  Since there is only one /table match, you will only ever have one <collection> in the output.

Also, your selection of top-level tuples (tuple elements that do not refer to a parent object) could be simplified.  Instead of:

select="tuple[not(tuple[@name='AssParentObjectRef']/atom[@name='irn'])]"

you could more simply say:

select="tuple[not(tuple/*)]"

since we can tell from your sample that top-level tuples only ever contain an empty self-closing <tuple> tag.

The next bit:

<xsl:key name="child" match="tuple" use="tuple[@name='AssParentObjectRef']/atom[@name='irn']" />

So we have a key matching on tuples and using the ID of the parent tuple.

<xsl:template match="tuple">
    <tuple>
        <xsl:copy-of select="atom"/>

        <!-- If this `tuple` is a parent (i.e. if it's included in
             the list of parent IDs in the key), then we add a
             wrapper for the children and process the children.  -->
        <xsl:if test="key('child', atom[@name='irn'])">
            <tuple name="children">
                <!-- Now we apply templates to the `tuple`s
                     in the key -->
                <xsl:apply-templates select="key('child', atom[@name='irn'])"/>
            </tuple>
        </xsl:if>
    </tuple>
</xsl:template>

This mostly works.  Comparing the output from this against your sample desired output, the bits you're missing are the <collection> wrapper tag (explained above), and the parent and top-level ancestor titles and IDs (for which you don't have any XSL code).

You state,

"And whilst this will group the records, the output is just one of these collections. So from a file of 3524 records, I get one collection of 24 records."

I can hypothesize that the XML structure of the remainder of your actual input might differ from what your XSL is targeting.  But without seeing your actual input, I cannot tell why this might be the case.

Your edit

You describe adding in the following template:

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

This is the "identity" template, so named because it simply copies over elements identically.  The duplication you're seeing is expected: the XSLT processor goes through the flat set of tuples in the input file and copies those over, and then also processes some of these within the context of the template specifically matching tuple, which applies templates to the tuples that matched the key (the nested children).

Alternative approach

The table template isn't too different:

<xsl:template match="table">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <!-- Target only parent-level `tuple`s.  This excludes
            child-level tuples, helping to prevent duplicates.-->
        <xsl:apply-templates select="tuple[not(tuple/*)]" mode="top"/>
    </xsl:copy>
</xsl:template>

Notably, we don't do anything with <collection> here -- we want to wrap each top-level tuple (and its children) in a <collection>, so we need to add that <collection> at the tuple level.

<!-- Add the `collection` wrapper only to top-level tuples -->
<xsl:template match="tuple" mode="top">
    <collection>
        <!-- Pass on this tuple to the main `tuple` template -->
        <xsl:apply-templates select="."/>
    </collection>
</xsl:template>

I match the tuple, but using a special mode -- we only want to add the <collection> wrapper for top-level tuples.  I then have a separate template that handles all tuples:

<!-- This is the main template for processing `tuple` elements.
    Most of the changes needed are common to all `tuple`s, so it
    makes sense to keep all the logic in one place. -->
<xsl:template match="tuple">
    <tuple>
        <!-- Copy each existing `atom` child -->
        <xsl:copy-of select="atom"/>
        <!-- Add in metadata about parent and top-level ancestor titles and IDs -->
            <xsl:choose>
                <!-- If this is a top-level item, just use its own values -->
                <xsl:when test="not(tuple/*)">
                    <atom name="TopTitle"><xsl:value-of select="atom[@name='EADUnitTitle']"/></atom>
                    <atom name="TopID"><xsl:value-of select="atom[@name='irn']"/></atom>
                </xsl:when>
                <!-- If this is a descendant, we need to find its parent and its top-level ancestor -->
                <xsl:when test="tuple/*">
                    <atom name="ParentTitle"><xsl:value-of select="tuple/atom[@name='EADUnitTitle']"/></atom>
                    <atom name="ParentID"><xsl:value-of select="tuple/atom[@name='irn']"/></atom>
                <!-- For convenience, grab the top-level ancestor `tuple` and stuff it in a variable.
                    This is vaguely annalogous to your use of `key`. -->

                <!-- Finding the top-level `tuple` is complicated by the fact that the ID values in
                    `<atom name="irn">` do not have a standardized format, other than that the whole
                    strings appear to consist of atomic values separated by single periods, with
                    descendant `irn` values appending to the precedent values.  Examples:
                      Top:        `da.04`
                      Descendant: `da.04.11.02`
                      Top:        `D42.PL.05`
                      Descendant: `D42.PL.05.01`
                    So chunking the ID values is a problematic approach, since we don't know how many
                    chunks comprise the initial non-numeric portion: `da`, or `D42.PL`, or ... ???.
                    Top-level elements *do* also have empty `<tuple name="AssParentObjectRef">` elements.
                    So we _can_ find all the top-level elements, and then look in those for the one that
                    has an `irn` value that matches the start of the `irn` value of this current `tuple`. -->
                <xsl:variable name="top" select="/table/tuple[tuple[@name='AssParentObjectRef'][not(*)]]
                    ['The above statement grabs all the `tuple`s that have an empty `tuple[@name=`AssParentObjectRef``.
                      The below statement then goes through all those `tuple`s to find the ones where the `irn`
                      values match the start of the `irn` value of the current `tuple`.']
                    [starts-with(current()/atom[@name='EADUnitID'], atom[@name='EADUnitID'])]"/>
                    <!-- Now we can reference that variable to get the top-level ancestor values -->
                    <atom name="TopTitle"><xsl:value-of select="$top/atom[@name='EADUnitTitle']"/></atom>
                    <atom name="TopID"><xsl:value-of select="$top/atom[@name='irn']"/></atom>
                </xsl:when>
            </xsl:choose>
        <!-- Process any children of this tuple, based on `irn` values.
            Basically, we look for any other `tuple`s in the `table`
            that point to this current `tuple`'s `irn` value. -->
        <xsl:if test="/table/tuple[tuple/atom[@name='irn'] = current()/atom[@name='irn']]">
            <tuple name="children">
                <xsl:apply-templates select="/table/tuple[tuple/atom[@name='irn'] = current()/atom[@name='irn']]"></xsl:apply-templates>
            </tuple>
        </xsl:if>
    </tuple>
</xsl:template>

I can't speak to your full dataset of 3524 records, but running the above against your sample input XML, my output is identical to your desired output (except for the one bug in your sample input XML about a referenced irn value, mentioned in a comment on your initial post).

Suggestions about your desired output XML

As a data format, your desired output XML has a number of aspects that strike me as a bit odd.

The <collection> wrapper

This seems superfluous; it's easy enough to see if a given tuple 1) is top-level, and 2) has any children.

The Parent and Top titles and IDs

These similarly seem superfluous.  So long as your data is structured hierarchically, all of this becomes clear without any need to specifically include it.  Including this metadata just duplicates already-available information.

The <tuple name="children"> wrapper

This could also be omitted without any loss of information.  The simple presence of one tuple element nested within another is evidence enough of children.

I don't know if you have any control over, or influence on, the design of the output XML file format, but if you do, I'd suggest a shorter and more streamlined structure, such as the following:

<table name="ecatalogue">
    <tuple>
        <atom name="irn">2470</atom>
        <atom name="EADUnitID">da.01</atom>
        <atom name="EADUnitTitle">Some title</atom>
        <tuple>
            <atom name="irn">5416</atom>
            <atom name="EADUnitID">da.01.01</atom>
            <atom name="EADUnitTitle">Child of Some title</atom>
        </tuple>
        <tuple>
            <atom name="irn">6</atom>
            <atom name="EADUnitID">da.01.02</atom>
            <atom name="EADUnitTitle">Child of Some title 2</atom>
            <tuple>
                <atom name="irn">8</atom>
                <atom name="EADUnitID">da.01.02.01</atom>
                <atom name="EADUnitTitle">3rd Generation</atom>
            </tuple>
        </tuple>
    </tuple>
</table>

In this structure, a tuple can only contain either atoms or other tuples.  We can identify a collection by simply finding any top-level tuple that contains other tuples.  We can identify children by simply finding any tuple that has a tuple parent.  We can find top-level and parent-level titles and IDs by simply picking a tuple and looking further up the element tree.

This structure is simpler, avoids data duplication, and is arguably clearer and easier to process.  That said, you know your needs!  Do what works for you.  :)

Please have a look through the code and comments, and let me know if you have any lingering questions.

Update 2017-06-15: Problem tuples

I added these to your previous sample input XML, and tried applying my previous XSL code.  In reviewing the problem tuples, two things occurred to me:

My previous code makes use of the tokenize function, which is only available in XSL 2.0 and newer.  Your post's tags did not specify XSL 1.0, and I failed to notice that you'd specified this in your sample XSL header.

I've reworked the XSL code above (the template that matches on tuple) to rely only on XSL 1.0 features.

The EADUnitID values in your initial sample input XML are not representative, so any attempt to code specifically against that sample is bound to fail when applied to your fuller, unseen input XML.

Your initial sample only includes EADUnitID values of the format da.XX, where XX is digits, and the .XX pattern can repeat.  I had made some assumptions about chunking this string and comparing the pieces.

However, your problem tuples have EADUnitID values of a very different format, which appears to be DXX.PL.XX, where again XX is digits and the .XX pattern on the end can repeat.  This means that relying on conformity of the chunks between periods is not a safe approach.

I've reworked the XSL code to instead match the entire front end of the irn string, which appears to work reliably.

Have a look at the code, and the comments, and let me know if anything remains unclear or non-functional.


来源: stackexchange

问:
Extract attachment from MIME email using Node.js 4.3
The NPM module "mailparser" (by Andris Reinman in Estonia) would be perfect for this. However, mailparser wants Node version 6.0 but I can only use Node version 4.3. This is because the e-mail parsing must execute within Amazon Web Services (AWS). Node version 4.3 is the only version AWS supports currently.</p>

答: It looks like AWS supports node 6.1 now</p>

ContentLicense: CC BY-SA 3.0

Body: It looks like AWS supports node 6.1 now


问:
Git (bitbucket) creating backup version
I am wondering what the standard procedure is to create an easy to find stable backup version of a git project. Ideally, it should not be necessary to go through the git logs, find a certain version and revert. Ideally, a version would be tagged. I saw git (I am using bitbucket) allows to create branches and some of those branches seem to have a tag release which In like because it indicates it is a stable version. Is branching out and switching back to the main branch for further a good way to manage this or are there better solutions available. I am not very familiar with git and its mechanics, so I would be happy to get some pointers towards good conventions.</p>

答:
Use Tags

You can use git-tag to create tags, with or without annotations. git tag --list will show you the available tags, and git-show can be used to view the annotation of a given tag. You then have to push the tags upstream, using git push --tags or similar. For most other purposes, you can treat a tag as a commitish for selecting commits. For example:

```<code>git tag --annotate --message="Release message 1" release01
git push --tags origin
# code ...
# code ...
git tag --annotate --message="Release message 2" release02
git push --tags origin
# code ...
# code ...
git tag --list
git show release02
# code ...
git reset --hard release01
</code>```

Pragmatically, you can use the name of a tag almost anywhere you could use a commit ID. You can use it to reset your branch, as a branch point to create new branches, and so forth.

Download Archives from Tags

In online services like Bitbucket, GitHub, or GitLab, every tag you create triggers an archive build, which you can download as a zipfile or tarball from the tags/releases widgets. In Bitbucket, you can navigate to your available downloads at:

https://bitbucket.org/<username>/<repository>/downloads/?tab=tags

In Bitbucket, you will see a screen with archives in zip, gzip, and bzip2 format for each tag in the project. Just click the format you want to download it.

This archive feature is common to most of the popular online Git hosts, but features and navigation to the archives may vary. To create the archives locally, just use git-archive from the command line instead.

Title: Git (bitbucket) creating backup version

Body:
Use Tags

You can use git-tag to create tags, with or without annotations. git tag --list will show you the available tags, and git-show can be used to view the annotation of a given tag. You then have to push the tags upstream, using git push --tags or similar. For most other purposes, you can treat a tag as a commitish for selecting commits. For example:

git tag --annotate --message="Release message 1" release01
git push --tags origin
# code ...
# code ...
git tag --annotate --message="Release message 2" release02
git push --tags origin
# code ...
# code ...
git tag --list
git show release02
# code ...
git reset --hard release01

Pragmatically, you can use the name of a tag almost anywhere you could use a commit ID. You can use it to reset your branch, as a branch point to create new branches, and so forth.

Download Archives from Tags

In online services like Bitbucket, GitHub, or GitLab, every tag you create triggers an archive build, which you can download as a zipfile or tarball from the tags/releases widgets. In Bitbucket, you can navigate to your available downloads at:

https://bitbucket.org/<username>/<repository>/downloads/?tab=tags

In Bitbucket, you will see a screen with archives in zip, gzip, and bzip2 format for each tag in the project. Just click the format you want to download it.

This archive feature is common to most of the popular online Git hosts, but features and navigation to the archives may vary. To create the archives locally, just use git-archive from the command line instead.


问:
Compiling go to Android: invalid signature of pthread_key_create
```<code>CGO_CFLAGS="--sysroot=android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/" CGO_ENABLED=1 CC=android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ CXX=android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ \
    GOOS=android GOARCH=arm go build -buildmode=c-archive --ldflags="--sysroot=android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/"
# runtime/cgo
/usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c: In function 'void inittls(void**, void**)':
/usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c:30:34: error: invalid conversion from 'void*' to 'void (*)(void*)' [-fpermissive]
  err = pthread_key_create(&k, nil);
                                  ^
In file included from /usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c:5:0:
/home/janus/Desktop/android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/usr/include/pthread.h:265:5: note: initializing argument 2 of 'int pthread_key_create(pthread_key_t*, void (*)(void*))'
 int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *));
</code>```

The same issue occurs with recent API levels like 22.

I am using the golang-1.8-go package mentioned on https://github.com/golang/go/wiki/Ubuntu

答:
The compiler command is incomplete. You can use gomobile build -x to see how to invoke the compiler correctly, remember you might need to quote the parameters correctly in your shell.

An example of a correct command:

GOMOBILE=/home/janus/go/pkg/gomobile GOPATH=$HOME/go ANDROID_HOME=/home/janus/Desktop/android-sdk-linux GOOS=android GOARCH=arm CC=$ANDROID_HOME/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang CXX=$ANDROID_HOME/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ CGO_CFLAGS="-target armv7a-none-linux-androideabi --sysroot $ANDROID_HOME/ndk-bundle/platforms/android-15/arch-arm -gcc-toolchain $ANDROID_HOME/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -I$GOMOBILE/include" CGO_CPPFLAGS="-target armv7a-none-linux-androideabi --sysroot $ANDROID_HOME/ndk-bundle/platforms/android-15/arch-arm -gcc-toolchain $ANDROID_HOME/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -I$GOMOBILE/include" CGO_LDFLAGS="-target armv7a-none-linux-androideabi --sysroot $ANDROID_HOME/ndk-bundle/platforms/android-15/arch-arm -gcc-toolchain $ANDROID_HOME/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 -L$ANDROID_HOME/ndk-bundle/platforms/android-15/arch-arm/usr/lib -L$GOMOBILE/lib/arm" CGO_ENABLED=1 GOARM=7 go build -pkgdir=$GOMOBILE/pkg_android_arm -buildmode=c-shared

Body:
CGO_CFLAGS="--sysroot=android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/" CGO_ENABLED=1 CC=android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ CXX=android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ \
    GOOS=android GOARCH=arm go build -buildmode=c-archive --ldflags="--sysroot=android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/"
# runtime/cgo
/usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c: In function 'void inittls(void**, void**)':
/usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c:30:34: error: invalid conversion from 'void*' to 'void (*)(void*)' [-fpermissive]
  err = pthread_key_create(&k, nil);
                                  ^
In file included from /usr/lib/go-1.8/src/runtime/cgo/gcc_android_arm.c:5:0:
/home/janus/Desktop/android-sdk-linux/ndk-bundle/platforms/android-9/arch-arm/usr/include/pthread.h:265:5: note: initializing argument 2 of 'int pthread_key_create(pthread_key_t*, void (*)(void*))'
 int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *));

The same issue occurs with recent API levels like 22.

I am using the golang-1.8-go package mentioned on https://github.com/golang/go/wiki/Ubuntu


问:
Is there a way to search for Top tweets with tweepy instead of latest tweets?
It seem tweepy's search function only accesses the latest tweets.  Is there a way to switch it so it searches the top tweets of a given query? Is there a workaround to retrieve the popular tweets if search cannot do this? I'm running OS X and python 3.6.

Thanks in advance.

答:
Along with your query topic, pass a result_type='popular' parameter to tweepy's search function. Although the tweepy documentation does not list this in its parameters, it is an available parameter in the Twitter Dev Docs.

```<code>popular_tweets = api.search(q='python', result_type='popular') # e.g. "python"
</code>```

popular : return only the most popular results in the response.

ContentLicense: CC BY-SA 4.0

Body:
Along with your query topic, pass a result_type='popular' parameter to tweepy's search function. Although the tweepy documentation does not list this in its parameters, it is an available parameter in the Twitter Dev Docs.

popular_tweets = api.search(q='python', result_type='popular') # e.g. "python"

popular : return only the most popular results in the response.


问:
Query Watson Discovery Service by Document ID
Is there a way to query by document id in WDS?  I've tried things like

id:"9cdd3db8-b127-4176-b727-b31bf86870fc"

id:9cdd3db8-b127-4176-b727-b31bf86870fc

Neither of these work.  I'm doing some testing, and I'd like to guarantee that I can get back a specific document.

答:
Use a filter parameter _id:9cdd3db8-b127-4176-b727-b31bf86870fc

reference on IBM DVW


问:
How to navigate to a route with parameters
I'm on a view with the current route:

http://localhost:4200/#/corp/projects/detail/05940c57/page1

And I want to get to

http://localhost:4200/#/corp/projects/detail/05940c57/page2

Is there a way to change to "page2" using navigate() or navigateByUrl()?

答:
It's not clear how did you arrive to the first URL, but assuming you have a configuration like

```<code>  {path: 'detail/:id/:page', component: ProjectDetailComponent}
</code>```

you can navigate to it as follows:

```<code>export class AppComponent {

  constructor (private router: Router) {}

  onClick () {
    this.router.navigate(['/detail', '05940c57', 'page2']);
  }
}
</code>```

Body:
It's not clear how did you arrive to the first URL, but assuming you have a configuration like

  {path: 'detail/:id/:page', component: ProjectDetailComponent}

you can navigate to it as follows:

export class AppComponent {

  constructor (private router: Router) {}

  onClick () {
    this.router.navigate(['/detail', '05940c57', 'page2']);
  }
}


问:
400 bad request (making game with node and socket.io)
Making a game, I have no idea what I am doing when it comes to the online aspect.

I am using node.js using my computer as the server host and client (localhost:3000)

```<code>var express = require('express'); //no idea what I am doing here
var app = express();
var server = app.listen(3000);
var socket = require("socket.io");
var io = socket(server);
io.sockets.on('connection', newConnection);
app.use(express.static("public"));

console.log("server is up"); //tells me that the server is ready

function newConnection(socket) {
    console.log("new player!", socket.id); //tells me when a new player connects.
}
</code>```

also have this code within the main public javascript file

```<code>var socket;
socket = io.connect("localhost:3000");
</code>```

Whenever a new player  connects i get 400 bad request errors and the game thinks multiple players have joined.

picture to aid

so pls help.

答:
In my game, I had a constructor function called "Number" and that was causing the problem the entire time.

I'm assuming that the socket.io or node.js already had a function called "Number" and that's what caused the problem.

Body:
Making a game, I have no idea what I am doing when it comes to the online aspect.

I am using node.js using my computer as the server host and client (localhost:3000)

var express = require('express'); //no idea what I am doing here
var app = express();
var server = app.listen(3000);
var socket = require("socket.io");
var io = socket(server);
io.sockets.on('connection', newConnection);
app.use(express.static("public"));

console.log("server is up"); //tells me that the server is ready

function newConnection(socket) {
    console.log("new player!", socket.id); //tells me when a new player connects.
}

also have this code within the main public javascript file

var socket;
socket = io.connect("localhost:3000");

Whenever a new player  connects i get 400 bad request errors and the game thinks multiple players have joined.

picture to aid

so pls help.


问:
How to control which JavaScript gets run after UpdatePanel partial postback endRequest?
I know I can hook into the client side events to run JavaScript after every partial postback; however, I want to do something like this:

```<code>protected void FooClicked(object sender, EventArgs e) {
        ClientScript.RegisterStartupScript(GetType(), "msg", "showMsg('Foo clicked');",true);
}
</code>```

I know I could totally hack it with hidden fields and run something after every postback, but there should be a pretty straightfoward way to in a similar fashion to this.

答: This is much more elegant, IMO.  apparently ASP AJAX will call any JS function named pageLoad. so write a function called pageLoad and it will be executed on every page load, even after partial update.</p>

ContentLicense: CC BY-SA 2.5

Body:
I know I can hook into the client side events to run JavaScript after every partial postback; however, I want to do something like this:

protected void FooClicked(object sender, EventArgs e) {
        ClientScript.RegisterStartupScript(GetType(), "msg", "showMsg('Foo clicked');",true);
}

I know I could totally hack it with hidden fields and run something after every postback, but there should be a pretty straightfoward way to in a similar fashion to this.


问:
Should I truncate my ints to shorts before computing bitwise ops on them?
Suppose I have a bitwise expression f: int -> int that is dependent only on the two lowest bytes of the input. In general, would it be any faster to compute (uint32)f'((uint16)x), where f': short -> short? Or is there overhead involved in the conversion? Assume f is too complicated for the compiler to figure this out on its own.

答:
```<code>int ifun ( int x )
{
    return(x-3);
}

short sfun ( short x )
{
    return(x-3);
}

unsigned short ufun ( unsigned short x )
{
    return(x-3);
}
</code>```

this is what we are talking about

```<code>00000000 <ifun>:
   0:   e2400003    sub r0, r0, #3
   4:   e12fff1e    bx  lr

00000008 <sfun>:
   8:   e2400003    sub r0, r0, #3
   c:   e1a00800    lsl r0, r0, #16
  10:   e1a00840    asr r0, r0, #16
  14:   e12fff1e    bx  lr

00000018 <ufun>:
  18:   e2400003    sub r0, r0, #3
  1c:   e1a00800    lsl r0, r0, #16
  20:   e1a00820    lsr r0, r0, #16
  24:   e12fff1e    bx  lr
</code>```

In order to properly honor the high level code which in the latter case is a 16 bit value on a 32 bit based target, something has to be done to clip the result.  But in this case values come in as 32 bit and go out as 32 bit.  so even though the input to the function may be a short the caller has clipped and sign extended the value to 32 bits so it could be just used by the next function.

another target

```<code>Disassembly of section .text:

00000000 <ifun>:
   0:   03e00008    jr  $31
   4:   2482fffd    addiu   $2,$4,-3

00000008 <sfun>:
   8:   2482fffd    addiu   $2,$4,-3
   c:   00021400    sll $2,$2,0x10
  10:   03e00008    jr  $31
  14:   00021403    sra $2,$2,0x10

00000018 <ufun>:
  18:   2482fffd    addiu   $2,$4,-3
  1c:   03e00008    jr  $31
  20:   3042ffff    andi    $2,$2,0xffff
</code>```

the shorter signed value cost more
    Disassembly of section .text:

```<code>0000000000000000 <ifun>:
   0:   8d 47 fd                lea    -0x3(%rdi),%eax
   3:   c3                      retq

0000000000000010 <sfun>:
  10:   8d 47 fd                lea    -0x3(%rdi),%eax
  13:   c3                      retq

0000000000000020 <ufun>:
  20:   8d 47 fd                lea    -0x3(%rdi),%eax
  23:   c3                      retq
</code>```

same instructions, the problem has to be solved elsewhere outside the function.

another target

```<code>Disassembly of section .text:

0000000000000000 <ifun>:
   0:   51000c00    sub w0, w0, #0x3
   4:   d65f03c0    ret

0000000000000008 <sfun>:
   8:   13003c00    sxth    w0, w0
   c:   51000c00    sub w0, w0, #0x3
  10:   d65f03c0    ret

0000000000000018 <ufun>:
  18:   53003c00    uxth    w0, w0
  1c:   51000c00    sub w0, w0, #0x3
  20:   d65f03c0    ret
</code>```

shorter cost more

a 16 bit target though

```<code>00000000 <_ifun>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   1d40 0004       mov 4(r5), r0
   8:   65c0 fffd       add $-3, r0
   c:   1585            mov (sp)+, r5
   e:   0087            rts pc

00000010 <_sfun>:
  10:   1166            mov r5, -(sp)
  12:   1185            mov sp, r5
  14:   1d40 0004       mov 4(r5), r0
  18:   65c0 fffd       add $-3, r0
  1c:   1585            mov (sp)+, r5
  1e:   0087            rts pc

00000020 <_ufun>:
  20:   1166            mov r5, -(sp)
  22:   1185            mov sp, r5
  24:   1d40 0004       mov 4(r5), r0
  28:   65c0 fffd       add $-3, r0
  2c:   1585            mov (sp)+, r5
  2e:   0087            rts pc
</code>```

the compiler considers int and short the same so we dont incur a cost, as we hoped, expected, get close or match the register size...

same for this 16 bit target

```<code>Disassembly of section .text:

00000000 <ifun>:
   0:   03 97           sbiw    r24, 0x03   ; 3
   2:   08 95           ret

00000004 <sfun>:
   4:   03 97           sbiw    r24, 0x03   ; 3
   6:   08 95           ret

00000008 <ufun>:
   8:   03 97           sbiw    r24, 0x03   ; 3
   a:   08 95           ret
</code>```

but dont match the target size and

```<code>long int ifun ( int x )
{
    return(x-3);
}

short sfun ( short x )
{
    return(x-3);
}
</code>```

as expected you incur a penalty.

```<code>00000000 <ifun>:
   0:   03 97           sbiw    r24, 0x03   ; 3
   2:   79 2f           mov r23, r25
   4:   68 2f           mov r22, r24
   6:   99 0f           add r25, r25
   8:   88 0b           sbc r24, r24
   a:   99 0b           sbc r25, r25
   c:   08 95           ret

0000000e <sfun>:
   e:   03 97           sbiw    r24, 0x03   ; 3
  10:   08 95           ret
</code>```

although it is up to the author of the backend, there is a reason why the native int variable type is there and what the spec, etc talks about it.  folks like to avoid that these days and use stdint for some reason, but at a cost.  yes you can lose portability by using native types.  performance, portability, maintenance, pick one or two you cant have all three.

Just examples of what Dai was saying, upvote that answer.  You were asking x86 specifically and in that case there was a sign penalty vs a size penalty.  Except for examples like this, I personally use unsigned unless there is some absolute reason I have to use signed...

EDIT

Bitwise helps greatly as does x86 to some extent (to reduce or erase the performance difference).  Just demonstrating the general concept (above) that smaller is not better if smaller is relative to the size of the primary operations/registers for that instruction set.  For an 8 or 16 bit machine, absolutely, more work is required if you use 32 bit variables even for bitwise operations.  Purely bitwise operations, I am trying to think of cases where the compiler would generate something different between native size and smaller, something that would matter.

When in doubt just try it and see (compile then disassemble is sometimes all you need).

x86 which you tagged, has some benefits over others, would need to do more research because with these simple examples the problem is being pushed off to code outside the function.  Can still make some differences though:

```<code>unsigned int ufun ( unsigned int x, unsigned int y )
{
    return(x*y)+3;
}

unsigned char ucfun ( unsigned char x, unsigned char y )
{
    return(x*y)+3;
}

0000000000000000 <ufun>:
   0:   0f af fe                imul   %esi,%edi
   3:   8d 47 03                lea    0x3(%rdi),%eax
   6:   c3                      retq

0000000000000010 <ucfun>:
  10:   89 f0                   mov    %esi,%eax
  12:   0f af c7                imul   %edi,%eax
  15:   83 c0 03                add    $0x3,%eax
  18:   c3                      retq
</code>```

so cant make the generalization that it is neither slower nor faster on x86.

EDIT2

I cant decide if this is a fair bitwise 32/16 bit comparison

```<code>typedef struct
{
    unsigned hello:5;
} MYSTRUCT;

unsigned int fun1 ( unsigned int x, MYSTRUCT m )
{
    return(x<<m.hello);
}

unsigned short fun2 ( unsigned short x, MYSTRUCT m )
{
    return(x<<m.hello);
}

00000000 <fun1>:
   0:   e201101f    and r1, r1, #31
   4:   e1a00110    lsl r0, r0, r1
   8:   e12fff1e    bx  lr

0000000c <fun2>:
   c:   e201101f    and r1, r1, #31
  10:   e1a00110    lsl r0, r0, r1
  14:   e1a00800    lsl r0, r0, #16
  18:   e1a00820    lsr r0, r0, #16
  1c:   e12fff1e    bx  lr
</code>```

hmm, yeah okay that was fair.

```<code>typedef struct
{
    unsigned hello:3;
} MYSTRUCT;

unsigned int fun1 ( unsigned int x, MYSTRUCT m )
{
    return(x<<m.hello);
}

unsigned short fun2 ( unsigned short x, MYSTRUCT m )
{
    return(x<<m.hello);
}

Disassembly of section .text:

00000000 <fun1>:
   0:   e2011007    and r1, r1, #7
   4:   e1a00110    lsl r0, r0, r1
   8:   e12fff1e    bx  lr

0000000c <fun2>:
   c:   e2011007    and r1, r1, #7
  10:   e1a00110    lsl r0, r0, r1
  14:   e1a00800    lsl r0, r0, #16
  18:   e1a00820    lsr r0, r0, #16
  1c:   e12fff1e    bx  lr
</code>```

Interesting x86 incurs a penalty...

```<code>0000000000000000 <fun1>:
   0:   89 f1                   mov    %esi,%ecx
   2:   89 f8                   mov    %edi,%eax
   4:   83 e1 07                and    $0x7,%ecx
   7:   d3 e0                   shl    %cl,%eax
   9:   c3                      retq

0000000000000010 <fun2>:
  10:   89 f1                   mov    %esi,%ecx
  12:   0f b7 c7                movzwl %di,%eax
  15:   83 e1 07                and    $0x7,%ecx
  18:   d3 e0                   shl    %cl,%eax
  1a:   c3                      retq
</code>```

okay nevermind arm masked the bits off as well, the shorter variable took an extra byte of machine code which gets into that why use xor rax,rax to zero a variable discussion...less machine code is cheaper and measurable but you may have to work pretty hard to do it (or not depends on how tricky you are)

Body:
int ifun ( int x )
{
    return(x-3);
}

short sfun ( short x )
{
    return(x-3);
}

unsigned short ufun ( unsigned short x )
{
    return(x-3);
}

this is what we are talking about

00000000 <ifun>:
   0:   e2400003    sub r0, r0, #3
   4:   e12fff1e    bx  lr

00000008 <sfun>:
   8:   e2400003    sub r0, r0, #3
   c:   e1a00800    lsl r0, r0, #16
  10:   e1a00840    asr r0, r0, #16
  14:   e12fff1e    bx  lr

00000018 <ufun>:
  18:   e2400003    sub r0, r0, #3
  1c:   e1a00800    lsl r0, r0, #16
  20:   e1a00820    lsr r0, r0, #16
  24:   e12fff1e    bx  lr

In order to properly honor the high level code which in the latter case is a 16 bit value on a 32 bit based target, something has to be done to clip the result.  But in this case values come in as 32 bit and go out as 32 bit.  so even though the input to the function may be a short the caller has clipped and sign extended the value to 32 bits so it could be just used by the next function.

another target

Disassembly of section .text:

00000000 <ifun>:
   0:   03e00008    jr  $31
   4:   2482fffd    addiu   $2,$4,-3

00000008 <sfun>:
   8:   2482fffd    addiu   $2,$4,-3
   c:   00021400    sll $2,$2,0x10
  10:   03e00008    jr  $31
  14:   00021403    sra $2,$2,0x10

00000018 <ufun>:
  18:   2482fffd    addiu   $2,$4,-3
  1c:   03e00008    jr  $31
  20:   3042ffff    andi    $2,$2,0xffff

the shorter signed value cost more
    Disassembly of section .text:

0000000000000000 <ifun>:
   0:   8d 47 fd                lea    -0x3(%rdi),%eax
   3:   c3                      retq

0000000000000010 <sfun>:
  10:   8d 47 fd                lea    -0x3(%rdi),%eax
  13:   c3                      retq

0000000000000020 <ufun>:
  20:   8d 47 fd                lea    -0x3(%rdi),%eax
  23:   c3                      retq

same instructions, the problem has to be solved elsewhere outside the function.

another target

Disassembly of section .text:

0000000000000000 <ifun>:
   0:   51000c00    sub w0, w0, #0x3
   4:   d65f03c0    ret

0000000000000008 <sfun>:
   8:   13003c00    sxth    w0, w0
   c:   51000c00    sub w0, w0, #0x3
  10:   d65f03c0    ret

0000000000000018 <ufun>:
  18:   53003c00    uxth    w0, w0
  1c:   51000c00    sub w0, w0, #0x3
  20:   d65f03c0    ret

shorter cost more

a 16 bit target though

00000000 <_ifun>:
   0:   1166            mov r5, -(sp)
   2:   1185            mov sp, r5
   4:   1d40 0004       mov 4(r5), r0
   8:   65c0 fffd       add $-3, r0
   c:   1585            mov (sp)+, r5
   e:   0087            rts pc

00000010 <_sfun>:
  10:   1166            mov r5, -(sp)
  12:   1185            mov sp, r5
  14:   1d40 0004       mov 4(r5), r0
  18:   65c0 fffd       add $-3, r0
  1c:   1585            mov (sp)+, r5
  1e:   0087            rts pc

00000020 <_ufun>:
  20:   1166            mov r5, -(sp)
  22:   1185            mov sp, r5
  24:   1d40 0004       mov 4(r5), r0
  28:   65c0 fffd       add $-3, r0
  2c:   1585            mov (sp)+, r5
  2e:   0087            rts pc

the compiler considers int and short the same so we dont incur a cost, as we hoped, expected, get close or match the register size...

same for this 16 bit target

Disassembly of section .text:

00000000 <ifun>:
   0:   03 97           sbiw    r24, 0x03   ; 3
   2:   08 95           ret

00000004 <sfun>:
   4:   03 97           sbiw    r24, 0x03   ; 3
   6:   08 95           ret

00000008 <ufun>:
   8:   03 97           sbiw    r24, 0x03   ; 3
   a:   08 95           ret

but dont match the target size and

long int ifun ( int x )
{
    return(x-3);
}

short sfun ( short x )
{
    return(x-3);
}

as expected you incur a penalty.

00000000 <ifun>:
   0:   03 97           sbiw    r24, 0x03   ; 3
   2:   79 2f           mov r23, r25
   4:   68 2f           mov r22, r24
   6:   99 0f           add r25, r25
   8:   88 0b           sbc r24, r24
   a:   99 0b           sbc r25, r25
   c:   08 95           ret

0000000e <sfun>:
   e:   03 97           sbiw    r24, 0x03   ; 3
  10:   08 95           ret

although it is up to the author of the backend, there is a reason why the native int variable type is there and what the spec, etc talks about it.  folks like to avoid that these days and use stdint for some reason, but at a cost.  yes you can lose portability by using native types.  performance, portability, maintenance, pick one or two you cant have all three.

Just examples of what Dai was saying, upvote that answer.  You were asking x86 specifically and in that case there was a sign penalty vs a size penalty.  Except for examples like this, I personally use unsigned unless there is some absolute reason I have to use signed...

EDIT

Bitwise helps greatly as does x86 to some extent (to reduce or erase the performance difference).  Just demonstrating the general concept (above) that smaller is not better if smaller is relative to the size of the primary operations/registers for that instruction set.  For an 8 or 16 bit machine, absolutely, more work is required if you use 32 bit variables even for bitwise operations.  Purely bitwise operations, I am trying to think of cases where the compiler would generate something different between native size and smaller, something that would matter.

When in doubt just try it and see (compile then disassemble is sometimes all you need).

x86 which you tagged, has some benefits over others, would need to do more research because with these simple examples the problem is being pushed off to code outside the function.  Can still make some differences though:

unsigned int ufun ( unsigned int x, unsigned int y )
{
    return(x*y)+3;
}

unsigned char ucfun ( unsigned char x, unsigned char y )
{
    return(x*y)+3;
}

0000000000000000 <ufun>:
   0:   0f af fe                imul   %esi,%edi
   3:   8d 47 03                lea    0x3(%rdi),%eax
   6:   c3                      retq

0000000000000010 <ucfun>:
  10:   89 f0                   mov    %esi,%eax
  12:   0f af c7                imul   %edi,%eax
  15:   83 c0 03                add    $0x3,%eax
  18:   c3                      retq

so cant make the generalization that it is neither slower nor faster on x86.

EDIT2

I cant decide if this is a fair bitwise 32/16 bit comparison

typedef struct
{
    unsigned hello:5;
} MYSTRUCT;

unsigned int fun1 ( unsigned int x, MYSTRUCT m )
{
    return(x<<m.hello);
}

unsigned short fun2 ( unsigned short x, MYSTRUCT m )
{
    return(x<<m.hello);
}

00000000 <fun1>:
   0:   e201101f    and r1, r1, #31
   4:   e1a00110    lsl r0, r0, r1
   8:   e12fff1e    bx  lr

0000000c <fun2>:
   c:   e201101f    and r1, r1, #31
  10:   e1a00110    lsl r0, r0, r1
  14:   e1a00800    lsl r0, r0, #16
  18:   e1a00820    lsr r0, r0, #16
  1c:   e12fff1e    bx  lr

hmm, yeah okay that was fair.

typedef struct
{
    unsigned hello:3;
} MYSTRUCT;

unsigned int fun1 ( unsigned int x, MYSTRUCT m )
{
    return(x<<m.hello);
}

unsigned short fun2 ( unsigned short x, MYSTRUCT m )
{
    return(x<<m.hello);
}

Disassembly of section .text:

00000000 <fun1>:
   0:   e2011007    and r1, r1, #7
   4:   e1a00110    lsl r0, r0, r1
   8:   e12fff1e    bx  lr

0000000c <fun2>:
   c:   e2011007    and r1, r1, #7
  10:   e1a00110    lsl r0, r0, r1
  14:   e1a00800    lsl r0, r0, #16
  18:   e1a00820    lsr r0, r0, #16
  1c:   e12fff1e    bx  lr

Interesting x86 incurs a penalty...

0000000000000000 <fun1>:
   0:   89 f1                   mov    %esi,%ecx
   2:   89 f8                   mov    %edi,%eax
   4:   83 e1 07                and    $0x7,%ecx
   7:   d3 e0                   shl    %cl,%eax
   9:   c3                      retq

0000000000000010 <fun2>:
  10:   89 f1                   mov    %esi,%ecx
  12:   0f b7 c7                movzwl %di,%eax
  15:   83 e1 07                and    $0x7,%ecx
  18:   d3 e0                   shl    %cl,%eax
  1a:   c3                      retq

okay nevermind arm masked the bits off as well, the shorter variable took an extra byte of machine code which gets into that why use xor rax,rax to zero a variable discussion...less machine code is cheaper and measurable but you may have to work pretty hard to do it (or not depends on how tricky you are)


问:
Submitting HTML array to jQuery AJAX
I have an HTML array and I want to submit it via jQuery ajax. When submitting and testing directly in the browser (just submitting the form as usual) I get the expected results and everything works. However, when submitting via ajax I get empty arrays.

I've tried serializing the form as well, but that produces the same result. Any thoughts as to what I'm doing wrong?

My form

```<code><form id="form-delete-previews" action="/delete/media/" method="DELETE">
@foreach($club->media as $image)
    <div class="col-xs-3">
        <input type="checkbox" id="cb{{ $loop->iteration }}" name="image[]" value="{{ $image->id }}">
        <label for="cb{{ $loop->iteration }}">
            <img src="{{ $image->getUrl('sm') }}" class="img-responsive img-rounded" alt="">
        </label>
    </div>
@endforeach
.....
</code>```

My javascript

```<code>$('#form-delete-previews').submit(function(e) {
    e.preventDefault();
    var form = $(this);
    formdata = new FormData(form[0]);
    swal({
        title: 'Are You Sure?',
        text: 'This can not be undone!',
        type: 'error',
        showCancelButton: true,
        confirmButtonColor: '#ee5254',
        confirmButtonText: 'Delete Images',
    }).then(function() {
        $.ajax({
            type: 'DELETE',
            url: form.attr('action'),
            headers: { 'X-CSRF-TOKEN': token },
            data: formdata,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(response) {
                success();
            },
            error: function(data) {
                oops();
            }
        });
    });
});
</code>```

My Laravel Controller

```<code>public function destroyMedia(Request $request)
{
    // return here for testing
    return response()->json([
        'type' => $request,
    ]);

    .....
}
</code>```

This is what's returned

```<code>{
    "type": {
        "attributes": {},
        "request": {},
        "query": {},
        "server": {},
        "files": {},
        "cookies": {},
        "headers": {}
    }
}
</code>```

UPDATE

Not sure why, but if I change the method from DELETE to POST it works.

Any idea why DELETE wouldn't work?

答: In your js checked the value of the formdata or try not to serialize the form and get it manually one by one</p>

Title: Submitting HTML array to jQuery AJAX

Body:
I have an HTML array and I want to submit it via jQuery ajax. When submitting and testing directly in the browser (just submitting the form as usual) I get the expected results and everything works. However, when submitting via ajax I get empty arrays.

I've tried serializing the form as well, but that produces the same result. Any thoughts as to what I'm doing wrong?

My form

<form id="form-delete-previews" action="/delete/media/" method="DELETE">
@foreach($club->media as $image)

        <input type="checkbox" id="cb{{ $loop->iteration }}" name="image[]" value="{{ $image->id }}">
        iteration }}">
            getUrl('sm') }}" class="img-responsive img-rounded" alt="">

@endforeach
.....

My javascript

$('#form-delete-previews').submit(function(e) {
    e.preventDefault();
    var form = $(this);
    formdata = new FormData(form[0]);
    swal({
        title: 'Are You Sure?',
        text: 'This can not be undone!',
        type: 'error',
        showCancelButton: true,
        confirmButtonColor: '#ee5254',
        confirmButtonText: 'Delete Images',
    }).then(function() {
        $.ajax({
            type: 'DELETE',
            url: form.attr('action'),
            headers: { 'X-CSRF-TOKEN': token },
            data: formdata,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function(response) {
                success();
            },
            error: function(data) {
                oops();
            }
        });
    });
});

My Laravel Controller

public function destroyMedia(Request $request)
{
    // return here for testing
    return response()->json([
        'type' => $request,
    ]);

    .....
}

This is what's returned

{
    "type": {
        "attributes": {},
        "request": {},
        "query": {},
        "server": {},
        "files": {},
        "cookies": {},
        "headers": {}
    }
}

UPDATE

Not sure why, but if I change the method from DELETE to POST it works.

Any idea why DELETE wouldn't work?


问:
Hook ZwDeviceIoControlFile with patchguard enabled on Win 10 x64
I need to change the output buffer whenever a call to ZwDeviceIoControlFile is made. Is it possible to hook ZwDeviceIoControlFile from within a kernel mode driver in Windows 10 x64 while Patchguard is enabled? If not, could I maybe use ObRegister callbacks to change its output buffer?

Would like to know if someone ever succeeded with this on Windows x64 while PG is enabled.

答: I ended up writing a Hypervisor and using EPT to hide the actual hook when PG does a read operation on the region. I am able to fully hook kernel APIs without PG noticing on Win 10 x64.</p>


问:
Whitespaces in AWS CloudFront for Signed URL's
I'm implementing Signed URL's with AWS CloudFront for my files in AWS S3, and my signed URL's are working fine for almost all of my files. The only ones that are not working are those files using whitespaces, e.g. "Hello world.txt"

As per documentation: Creating a Signed URL Using a Custom Policy

Concatenate the following values in the specified order, and remove the whitespace (including tabs and newline characters) between the parts.

I have a lot of files already stored in AWS S3 and many of them are using whitespaces. Is there any way that I can use Signed URL's for those files?

The signed URL generated is already using %20%. Even i tried to change %20% by + and didn't work.
Any help will be appreciated. Thanks.

答:
Ok... It's working now.

The resource in the policy shouldn't include whitespaces. Therefore, previous to encode the policy, all whitespaces in the Resource should be replaced by '+'. E.g. "Hello+world.txt"

Hope this helps someone.


问:
How to remove value from Multidimensional Array based on Key and Value - PHP
How can I remove value "288" from "pecan" and return the updated array.

Array like so:

```<code>a: 10: {
s: 13: "Fraser Island";
a: 7: {
    i: 0;
    i: 303;
    i: 1;
    i: 34;
    i: 2;
    i: 27;
    i: 3;
    i: 291;
    i: 4;
    i: 293;
    i: 5;
    i: 37;
    i: 6;
    i: 288;
}
s: 5: "pecan";
a: 5: {
    i: 0;
    i: 34;
    i: 1;
    i: 27;
    i: 2;
    i: 291;
    i: 3;
    i: 59;
    i: 4;
    i: 288;
}
s: 11: "New Triper";
a: 3: {
    i: 0;
    i: 291;
    i: 1;
    i: 293;
    i: 2;
    i: 288;
}
</code>```

}

Which translates to this in PHP:

```<code>Array (
[0] => Array (
    [Fraser Island] => Array (
        [0] => 303
        [1] => 34
        [2] => 27
        [3] => 291
        [4] => 293
        [5] => 37
        [6] => 288
    )
    [pecan] => Array (
        [0] => 34
        [1] => 27
        [2] => 291
        [3] => 59
        [4] => 288
    )
    [New Triper!] => Array (
        [0] => 291
        [1] => 293
        [2] => 288
    )
)
</code>```

)

Delete/remove a value from a multidimensional array by finding the array column name and the value. I use this code:

```<code>// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $val) {
    $return = array();
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val) continue;
            $return[$k] = $v;
        }
   return $return;
}
</code>```

However it removes all matching values from all array columns.

答:
This function will remove Element From specific Indexed/Associative array:

```<code>function removeElement($array, $value) {
  if(is_array($array)) {
    $key = array_search($value,$array);
       if(!isset($key)) {
         unset($array[$key]);
         return $array;
       }
  }
}
</code>```

This function will remove each element if the value matches from a multidimensional array:

```<code>function removeElementMulti($array, $value) {
   if(is_array($array)) {
      foreach($array as $array_key => $simple_array) {
         $key = array_search($value,$simple_array);
         if(isset($key)) {
            unset($array[$array_key][$key]);
         }
      }

      return $array;
   }
}
</code>```

Body:
How can I remove value "288" from "pecan" and return the updated array.

Array like so:

a: 10: {
s: 13: "Fraser Island";
a: 7: {
    i: 0;
    i: 303;
    i: 1;
    i: 34;
    i: 2;
    i: 27;
    i: 3;
    i: 291;
    i: 4;
    i: 293;
    i: 5;
    i: 37;
    i: 6;
    i: 288;
}
s: 5: "pecan";
a: 5: {
    i: 0;
    i: 34;
    i: 1;
    i: 27;
    i: 2;
    i: 291;
    i: 3;
    i: 59;
    i: 4;
    i: 288;
}
s: 11: "New Triper";
a: 3: {
    i: 0;
    i: 291;
    i: 1;
    i: 293;
    i: 2;
    i: 288;
}

}

Which translates to this in PHP:

Array (
[0] => Array (
    [Fraser Island] => Array (
        [0] => 303
        [1] => 34
        [2] => 27
        [3] => 291
        [4] => 293
        [5] => 37
        [6] => 288
    )
    [pecan] => Array (
        [0] => 34
        [1] => 27
        [2] => 291
        [3] => 59
        [4] => 288
    )
    [New Triper!] => Array (
        [0] => 291
        [1] => 293
        [2] => 288
    )
)

)

Delete/remove a value from a multidimensional array by finding the array column name and the value. I use this code:

// DELETE VALUE FROM ARRAY
function remove_element_by_value($arr, $val) {
    $return = array();
    foreach($arr as $k => $v) {
        if(is_array($v)) {
            $return[$k] = remove_element_by_value($v, $val); //recursion
            continue;
        }
        if($v == $val) continue;
            $return[$k] = $v;
        }
   return $return;
}

However it removes all matching values from all array columns.

Body:
This function will remove Element From specific Indexed/Associative array:

function removeElement($array, $value) {
  if(is_array($array)) {
    $key = array_search($value,$array);
       if(!isset($key)) {
         unset($array[$key]);
         return $array;
       }
  }
}

This function will remove each element if the value matches from a multidimensional array:

function removeElementMulti($array, $value) {
   if(is_array($array)) {
      foreach($array as $array_key => $simple_array) {
         $key = array_search($value,$simple_array);
         if(isset($key)) {
            unset($array[$array_key][$key]);
         }
      }

      return $array;
   }
}


问:
Sort function isn't working
I have a dynamic range rng2 that goes from A4:F?. I tried adapting code from this site, but keep getting errors when I try to sort it. Officers is a string "E1:E" & last row. Tgroup is a string "D1:D" & last row.

```<code>rng2.Sort key1:=Range(officers), Order1:=xlAscending, _
key2:=Range(tgroup), order2:=x1Ascending, Header:=x1No
</code>```

From comments:

i is a counter used earlier & is 1 row below the last row.

```<code>Dim officers As String, tgroup As String, sArea As String, rng2 As Range
officers = "E1:E" & (i - 4)
tgroup = "D1:D" & (i - 4)
sArea = "A4:F" & (i - 1)
Set rng2 = rng.Range(sArea)
rng2.Sort key1:=Range(officers), Order1:=xlAscending, _
          key2:=Range(tgroup), order2:=xlAscending, Header:=xlNo
</code>```

rng is a range from A1:F? but has merged cells which is why I have rng2.

答:
The reason the sort didn't work correctly isn't as @YowE3k suggested. The answer is that I wasn't accurately defining the range portion of the sort. The solution is:

```<code>    rng2.Sort key1:=rng2.Range(officers), Order1:=xlAscending, _
        key2:=rng2.Range(tgroup), order2:=xlAscending, Header:=xlNo
</code>```

I wasn't using the rng2.Range so it was getting the wrong area & couldn't sort.

Title: Sort function isn't working

Body:
I have a dynamic range rng2 that goes from A4:F?. I tried adapting code from this site, but keep getting errors when I try to sort it. Officers is a string "E1:E" & last row. Tgroup is a string "D1:D" & last row.

rng2.Sort key1:=Range(officers), Order1:=xlAscending, _
key2:=Range(tgroup), order2:=x1Ascending, Header:=x1No

From comments:

i is a counter used earlier & is 1 row below the last row.

Dim officers As String, tgroup As String, sArea As String, rng2 As Range
officers = "E1:E" & (i - 4)
tgroup = "D1:D" & (i - 4)
sArea = "A4:F" & (i - 1)
Set rng2 = rng.Range(sArea)
rng2.Sort key1:=Range(officers), Order1:=xlAscending, _
          key2:=Range(tgroup), order2:=xlAscending, Header:=xlNo

rng is a range from A1:F? but has merged cells which is why I have rng2.

Body:
The reason the sort didn't work correctly isn't as @YowE3k suggested. The answer is that I wasn't accurately defining the range portion of the sort. The solution is:

    rng2.Sort key1:=rng2.Range(officers), Order1:=xlAscending, _
        key2:=rng2.Range(tgroup), order2:=xlAscending, Header:=xlNo

I wasn't using the rng2.Range so it was getting the wrong area & couldn't sort.


问:
Import from CSV to Mysql round up digits
I have a data in CSV file when i import into Mysql automatically sets the data type varchar and round up the digits forexample in CSV. if the value is 15.563 after the import it looks like 15.56. what technique do i use to avoid this?

答:
MySQL doesn't automatically do anything like that, it just conforms to whatever your schema is as its defined, and if that's VARCHAR, then so is the data that's imported.

If you need three places of precision you should probably be using a DECIMAL(10,3) or something similar depending on how many significant digits you want in total. 10,3 allows for N,NNN,NNN.NNN.


问:
How to reference properties of a class from within a callback
Given the following class:

```<code>class TimerFunk {
    constructor(someObject){
        this.a = 1
        this.someObject = someObject
    }
    funk(){
        console.log(this.a)
        if(this.a > 2){
            return
        }
        this.a++
        this.someObject.execCallback(this.funk)
    }
}
</code>```

When the following commands are executed:

```<code>t = new TimerFunk({execCallback:function(callback){callback()}})
t.funk()
</code>```

it is intended that the property TimerFunk.a is logged to the console until this.a>2. However instead the loop only executes once:

```<code>VM2097:9 1
VM2097:9 Uncaught TypeError: Cannot read property 'a' of undefined
    at funk (<anonymous>:9:25)
    at Object.execCallback (<anonymous>:5:13)
    at TimerFunk.funk (<anonymous>:13:25)
    at <anonymous>:1:3
</code>```

From debugging I have figured out that this is because on the first loop this represents the object t however, on the 2nd loop this represents someObject.

Is there any way I can fix this and allow me to access class properties from within the executed callback?

答:
Your callback is called without the TimerFunk instance t as the this object:

```<code>function(callback){callback()}}
</code>```

A solution is to bind the this object of the callback to the TimerFunk instance:

```<code>funk() {
  ...
  this.someObject.execCallback(this.funk.bind(this));
}
</code>```

Alternatively, you can explicitly pass t as the this object via callback.call(t):

```<code>t = new TimerFunk({execCallback:function(callback){callback.call(t)}});
</code>```

Body:
Given the following class:

class TimerFunk {
    constructor(someObject){
        this.a = 1
        this.someObject = someObject
    }
    funk(){
        console.log(this.a)
        if(this.a > 2){
            return
        }
        this.a++
        this.someObject.execCallback(this.funk)
    }
}

When the following commands are executed:

t = new TimerFunk({execCallback:function(callback){callback()}})
t.funk()

it is intended that the property TimerFunk.a is logged to the console until this.a>2. However instead the loop only executes once:

VM2097:9 1
VM2097:9 Uncaught TypeError: Cannot read property 'a' of undefined
    at funk (<anonymous>:9:25)
    at Object.execCallback (<anonymous>:5:13)
    at TimerFunk.funk (<anonymous>:13:25)
    at <anonymous>:1:3

From debugging I have figured out that this is because on the first loop this represents the object t however, on the 2nd loop this represents someObject.

Is there any way I can fix this and allow me to access class properties from within the executed callback?

Body:
Your callback is called without the TimerFunk instance t as the this object:

function(callback){callback()}}

A solution is to bind the this object of the callback to the TimerFunk instance:

funk() {
  ...
  this.someObject.execCallback(this.funk.bind(this));
}

Alternatively, you can explicitly pass t as the this object via callback.call(t):

t = new TimerFunk({execCallback:function(callback){callback.call(t)}});


问:
Refreshing a DataGridView binded with a access database
I have created in Visual Basic .net a new DataGridView in my form. I have used the wizard to show some fields of a table in my access database.
I would like to add a refresh button and force the datagrid to load the data again from the database, but I'm not sure how to do that. I have tried several refresh method but it does not work.
Can anybody help me?</p>

答:
You need to bind the datasource again. I've never done this with an Access database but here's how you might do it using ADO.NET with SQL

```<code>Using command As New SqlCommand("CommandText Goes Here", "ConnectionString goes here")
    command.CommandType = CommandType.Text
    Dim ds As New DataSet("Results")
    Using da As New SqlDataAdapter(command)
        da.Fill(ds)
        myGrid.DataSource = ds
    End Using
End Using
</code>```

So you could wrap that up in a button click event.

There are a ton of other ways of doing this but that works for me.

Body:
You need to bind the datasource again. I've never done this with an Access database but here's how you might do it using ADO.NET with SQL

Using command As New SqlCommand("CommandText Goes Here", "ConnectionString goes here")
    command.CommandType = CommandType.Text
    Dim ds As New DataSet("Results")
    Using da As New SqlDataAdapter(command)
        da.Fill(ds)
        myGrid.DataSource = ds
    End Using
End Using

So you could wrap that up in a button click event.

There are a ton of other ways of doing this but that works for me.