Paul - The Programmer

simple & stupid

Auto format XML file

1. Format XML file with VIM.

VIM's auto indent feature supports format the XML files. But generally this feature may be disabled by default. 

To enable the auot indent feature, just add the below options in the .vimrc in user's home directory.

filetype indent on

Next time when the xml file be opened by VIM, the xml file type's indent script will be auotmatically loaded as well. In my environment, the indent script for xml is /usr/share/vim/vim72/ftplugin/xml.vim. The xml.vim's path may vary in different system.

To format the XML contents, just use the vim command in the command mode:

gg=G

This command will reformat the entire contents.

2. Fomart XML file with xmllint.

The xmllint are generally used for validating the xml file syntax. But it also has the ability to format and align the XML documents. The XMLLINT_INDENT environmnet variable controls the indentation. The default value is two spaces ( "  " ).

Basically, open the xml file with VIM then execute the below vim command in command mode:

 

:%!xmllint --format %

This command means the xmllint be excuted with the entire xml document contents as input, then the result of xmllint will be used to update the content of current buffer.

The xmllint also do some refine for the xml document, such as, adding the xml version declaration in the document's head.

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.