Showing page 7 of 38.


 

Cleaning up XML using VIM

Here is a quick function for cleaning up XML without any line breaks. This is a quick and dirty solution with some minor issues (e.g. turning <test></test> to <test>\n</test>), but the goal of this it not to be too accurate, but to quickly put a non-readable XML into a readable form for reference.

1
2
3
4
5
6
function! PrettifyXML()
	set ft=xml
	:%s/></>\r</g
	:0
	:norm =G
endfunction

Put this in your vimrc file and call it using :call PrettifyXML()

Read more

Error installing "Windows 7 service pack 1", code 80200053

Microsoft recently released the Service Pack 1 for Windows 7. When installing this through Windows Update it would keep failing and give me “code 80200053.” After looking around for a bit I found a solution that fixed this.

  1. Go to the Start menu and type “services.msc” and press enter.
  2. Find “Windows Update” service, right click on it and click “Restart.”
  3. Go to the Start menu and type “Windows Update” and try installing the service pack.
Read more

Compiling what...?!!

Lol, I saw this earlier while compiling Samba4. It says “Compiling torture...” I wonder what it does :)

Compiling what!?!

Read more

New plumbing is in place

I finally got some time to move my site to the jQuery Javascript library from Prototype. Why? Because lately most of my projects have been using jquery so I have been using and learning it a lot more than Prototype, and I am starting to understand it very well. You might also notice the LavaLamp jQuery plugin that I am using for the navigation menu above. Initially my intention was to create that functionality myself, but later decided to just that library to save some time for other projects.

Read more

Preventing namespaces from showing up in XSLT output

I recently had the need to output a SELECT HTML element with a list of states. My initial approach was simple where I used <xsl:copy>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:custom="com.thebitguru" exclude-result-prefixes="custom">
	<xsl:output method="html" omit-xml-declaration="yes"/>
	<xsl:strip-space elements="*" />

	<custom:states>
		<option value="AL">Alabama</option>
		<!-- ... -->
		<option value="WI">Wisconsin</option>
		<option value="WY">Wyoming</option>
	</custom:states>

	<xsl:template match="option">
		<xsl:copy>
			<xsl:if test="@value = $val">
				<xsl:attribute name="selected">selected</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="value"><xsl:value-of select="@value" /></xsl:attribute>
			<xsl:value-of select="." />
		</xsl:copy>
	</xsl:template>

	<xsl:template match="*">
		<label for="state">* State:</label>
		<select name="state" id="state">
			<xsl:apply-templates select="document('')/*/custom:states/option" />
		</select>
	</xsl:template>
</xsl:stylesheet>

The problem with this approach was that the option tags were printing the xml namespaces, even with the exclude-result-prefixes="custom" specified (because that attribute does not apply to <xsl:copy>).

1
2
3
4
<option value="AL" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="com.thebitguru">Alabama</option>
...
<option value="WI" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="com.thebitguru">Wisconsin</option>
<option value="WY" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:custom="com.thebitguru">Wyoming</option>

I didn’t want these namespaces in the output so after some searching and reading through the docs I decided to create new elements using the <xsl:element> tags instead, especially since the tag that I was copying was very small.

1
2
3
4
5
6
7
8
9
<xsl:template match="option">
	<xsl:element name="option">
		<xsl:if test="@value = $val">
			<xsl:attribute name="selected">selected</xsl:attribute>
		</xsl:if>
		<xsl:attribute name="value"><xsl:value-of select="@value" /></xsl:attribute>
		<xsl:value-of select="." />
	</xsl:element>
</xsl:template>

There are other ways to accomplish the same thing, e.g. copy-namespaces in XSLT 2.0, but in my case I was stuck with XSLT 1.0.

Read more

Quick vim tip: Copying matching lines to the end of file

I recently had the need to cleanup a long diff file and move certain lines to the end of the file for further analysis. The diff file that I got was something like the following, only much longer (produced using diff -qr xxx yyy).

Files xxx/abc1 and yyy/abc1 differ
Only in xxx: cde1
Files xxx/abc3 and yyy/abc3 differ
Files xxx/abc4 and yyy/abc4 differ
Only in xxx: cde2
Only in xxx: cde5
Files xxx/abc5 and yyy/abc5 differ
Only in xxx: cde3

I didn’t care as much about the “Only…” lines, but the files that differed needed more attention. To accomplish this I wanted to get this file in the following format.

Only in xxx: cde1
Only in xxx: cde2
Only in xxx: cde5
Only in xxx: cde3
Files xxx/abc1 and yyy/abc1 differ
Files xxx/abc3 and yyy/abc3 differ
Files xxx/abc4 and yyy/abc4 differ
Files xxx/abc5 and yyy/abc5 differ

I could go in and copy and paste all the lines that matched, but that would be a lot of manual work. So, I looked around for a minute and came up with the following command.

:%g/^Files/m$

Vim is awesome!

Read more

Play Button iTunes Patch updated

My original patch possibly worked only with OS X 10.6.5. Last night I figured out a better way to apply the patch without relying on a pre-supplied diff. What this means is that starting with version 0.7 the patch should work with pretty much all versions.

If you are interested, check it out on the project page.

Read more