Paul - The Programmer

simple & stupid

XPath error: Cannot select a node here: the context item is an atomic value

Assume the input xml file is like

<people>
    <person>
        <name>paul</name>
        <gender>male</gender>
    </person>
    <person>
        <name>lee</name>
        <gender>female</gender>
    </person>
</people>

This XPath error complained for the below style sheet. 

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="team">
                <xsl:for-each select="distinct-values(//gender)">
                    <xsl:variable name="gender" select="."/>
                    <xsl:element name="{$gender}">
                        <xsl:for-each select="//person[gender=$gender]"> <!-- Wrong! A node is selected for actomic context item. -->
                            <xsl:variable name="name" select="name"/>
                            <xsl:element name="{$name}"/> 
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

This error means that a node was selected for a actomic context item. The atomic context item is chosen by the outer for-each loop <xsl:for-each select="distinct-values(//gender)"/>.

Node items can not be selected in this loop context.

The solution is define a variable outside of loop. the variable value is the string of the node path. Then select the node with that variable.

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:element name="team">
            <xsl:variable name="person" select="//person"/> <!-- Define the variable with the node path. -->
                <xsl:for-each select="distinct-values(//gender)">
                    <xsl:variable name="gender" select="."/>
                    <xsl:element name="{$gender}">
                        <xsl:for-each select="$person[gender=$gender]"> <!-- Select the node by the variable defined outside of this for-each loop. -->
                            <xsl:variable name="name" select="name"/>
                            <xsl:element name="{$name}"/> 
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Weird enough, but seems like the only available solution. 

Good Linux tool, netcat

Netcat, as know as nc, is a simple command line tool to send and receive data with TCP or UDP protocal.

There are many tutorials for the netact on the internet, since it has been wildly used as a back-end tool to :

  • Simulate server
  • Scanning ports
  • File transfers
  • Proxy gateway
  • Many other things to transfer data with TCP/UDP from one place to another.

Hence here I'd like to show you something different, an example of how to create a echo server with netcat.

The server side just as simple as one line command:

$ nc -l -p 12345 -c 'while true; do read i && echo [echo] $i; done'

Then the telnet can be used as a client to connect to the nc server

$ telnet localhost 12345
hello
[echo] hello

Once you input any string, the server returns your input with [echo] prefix.

Good Linux tool, Module Assistant

Module Assistant is a powerful tool to build Debian kernel modules without re-compile the whole linux kernel. Module Assitant extramly facilitate the process of building kenerl modules by automatically download the module source code from Debian package repository and compile the source code to generate kernel module binary file. 

Moduel Assistant requires to run as root. With Module Assistant, the process of building kernel module is simplyfied to 5 steps.

- Install Module Assistant, 

$ sudo apt-get install module-assistant

- Prepare the artifact for building kernel modules,

$ sudo m-a prepare

The command will automatically download and install the kernel headers corresponding to current kernel and needed building tools like gcc, make and so on.

- Update the module assitant suppored module list,

$ sudo m-a update

-  Check supported module list,

$ sudo m-a list

- Build the needed module, such as, the module for Logitech Quickcam,

$ sudo m-a a-i qc-usb-source

Option a-i stands for automatic installation.

Good Linux tool, Network Manager.

Network Manager is a GUI application powered by dbus for network configuration. Network Manager provides a user-friendly interface to set up the wired and wireless network interface.

Network Manager consists of a background daemon process running as root and a front-end applet application.

Install Network Manager with command,

$ sudo apt-get install network-manager network-manager-gnome

To utilize the Network Manager, for Debian users, the user has to be a member of netdev group. For other distribution users, the similar user privileges are required.

$ sudo usermod -a -G netdev paul

Verify the user group setting with

$ id paul
uid=1000(paul) gid=1000(paul) groups=1000(paul),4(adm),24(cdrom),25(floppy),29(audio),44(video),46(plugdev),109(netdev),110(bluetooth),116(scanner)

By default, Network Manager only handles the interface not declared in /etc/network/interfaces.

Not declared interface means the interface is not described in the interface or the interface is disableed with a #NetworkMangaer# prefix.

For instance,

# The primary network interface
allow-hotplug eth0
#NetworkManager#iface eth0 inet dhcp

To enable Network Manager to handle the interfaces in /etc/network/interfaces, the Network Manager's configuration file /etc/NetworkManager/NetworkManager.conf should be updated as:

[main]
plugins=ifupdown,keyfile

[ifupdown]
managed=true

Then restart network-manager service:

$ sudo /etc/init.d/network-manager restart

 

Remove empty lines by VIM global command

The VIM global command can select the lines that match a certain pattern in the scope of whole file. And the appened commands can operate on the selected lines.

Either the following command can be used to remove the empty lines.

:g/^\(\t\|\s\)*$/d
:v/\S/d

The command ':v' is similar to the command 'grep -v' which select the inverted matched lines.

\S is the meta character for the characters which is not white space or tab.