<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Xml</title>
	<atom:link href="http://huddledmasses.org/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>A DSL for XML in PowerShell: New-XDocument</title>
		<link>http://huddledmasses.org/a-dsl-for-xml-in-powershell-new-xdocument/</link>
		<comments>http://huddledmasses.org/a-dsl-for-xml-in-powershell-new-xdocument/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 05:52:12 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Atom]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Generator]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1406</guid>
		<description><![CDATA[In July of last year I wrote a PowerShell script with the goal of allowing me to generate XML from PowerShell with a simple markup that would look a little like the resulting XML ... this week I was using that script again, and had a couple of issues that made me go back and [...]]]></description>
			<content:encoded><![CDATA[	<p>In July of last year <a href="http://poshcode.org/1233">I wrote a PowerShell script</a> with the goal of allowing me to generate <span class="caps">XML</span> from PowerShell with a simple markup that would look a little like the resulting <span class="caps">XML</span> ... this week I was using that script again, and had a couple of issues that made me go back and look at the source.</p>

	<p>While I was playing with the source and tweaking things a little bit to improve the way it handles namespaces, I started playing with the idea that I could improve the syntax. At the very least, I thought, I ought to be able to do away with all those &#8220;xe&#8221; aliases&#8230;</p>

	<p>Well, <a href="http://poshcode.org/1682">I was able to</a>.  ( <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <a href="http://poshcode.org/1888">new version</a>) And what&#8217;s more, I managed to dramatically clean up the way namespaces work, and make it so that really, the only ugly part of the syntax is the initial declaration of namespaces! I&#8217;m going to start with two examples, and use them to walk you through the features  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<h3>Example 1</h3>

	<p>The simplest example I could think of is to list all the files in a folder, with the file size and last modified stamp:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">XDocument</span></span> folder <span style="color: #000066;">-path</span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; file <span style="color: #000066;">-Modified</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">LastWriteTimeUtc</span> <span style="color: #000066;">-Size</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">Length</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">Name</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>The output of that, when run on my formats folder, looks like this:</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;folder</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;C:\Users\Jaykul\Documents\WindowsPowerShell\formats&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:00Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;30474&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>CliXml.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.48001Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;14314&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>format.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2010-01-16T21:30:06.0562796Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;18275&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>NppExternalLexers.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-03-18T21:28:51.6579351Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;5802&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Recommender.Types.Format.ps1xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;file</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.518029Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;5107&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>types.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/folder<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>You can immediately see what the script does: New-XDocument (which is aliased as &#8216;xml&#8217;) actually generates the root xml node, so the first argument to it is the name of that node, and any other arguments become attributes &#8230; except for the script block. That script block turns into the <em>contents</em> of the node.</p>

	<p>Inside the script block, PowerShell code is parsed as usual, but whenever a command that doesn&#8217;t exist is encountered, it is turned into an xml node! Pretty simple, right?  Of course, if you wanted to create a node with a name that&#8217;s already taken by a PowerShell command, you can just replace <code>file</code> with <code>New-XElement file</code>, or (using aliases) <code>xe &#39;file&#39;</code>, which explicitly creates an xml node with the given name.</p>

	<p>That&#8217;s pretty much it for our first example, so let&#8217;s look at a more complicated example, with multiple namespaces, and deeper nesting.</p>

	<h3>Example 2</h3>

	<p>This time, we&#8217;ll create an Atom document, and we&#8217;ll include some namespace extensions (including a made up one for listing my files as we did above):</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">XDocument</span></span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>XNamespace<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #009900;">&quot;feed&quot;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-fi</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>XNamespace<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;http://huddledmasses.org/schemas/FileInfo&quot;</span><span style="color: #333;">&#41;</span> &nbsp; `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-dc</span> <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>XNamespace<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;http://purl.org/dc/elements/1.1&quot;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">-</span>$<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>XNamespace<span style="color: #333;">&#93;</span></span>::<span style="color: #003366; font-weight: bold;">Xml</span> <span style="color: #66cc66;">+</span><span style="color: #009900;">'lang'</span><span style="color: #333;">&#41;</span> <span style="color: #009900;">&quot;en-US&quot;</span> <span style="color: #000066;">-Encoding</span> <span style="color: #009900;">&quot;UTF-16&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;`<br />
<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;title <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;Huddled Masses: You can do more than breathe for free...&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;link <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;http://HuddledMasses.org/&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;updated <span style="color: #333;">&#123;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span> <span style="color: #000066;">-f</span> u<span style="color: #333;">&#41;</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot; &quot;</span>,<span style="color: #009900;">&quot;T&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;author <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; name <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;Joel Bennett&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; uri <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;http://HuddledMasses.org/&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;id <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;http://HuddledMasses.org/&quot;</span> <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp;entry <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; title <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;A DSL for XML in PowerShell: New-XDocument&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; link <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;http://HuddledMasses.org/A-DSL-for-XML-in-PowerShell-New-XDocument/&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; id <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;http://HuddledMasses.org/A-DSL-for-XML-in-PowerShell-New-XDocument/&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; updated <span style="color: #333;">&#123;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Date</span></span> <span style="color: #cc66cc;">2010</span><span style="color: #66cc66;">/</span>03<span style="color: #66cc66;">/</span>03 <span style="color: #000066;">-f</span> u<span style="color: #333;">&#41;</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot; &quot;</span>,<span style="color: #009900;">&quot;T&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; summary <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;A while back, I posted a simple mini language for generating XML from PowerShell script. However, I was using it the other day, and I really just felt that the markup was ugly, since it was littered with 'xe' marks and such.&quot;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; link <span style="color: #000066;">-rel</span> license <span style="color: #000066;">-href</span> <span style="color: #009900;">&quot;http://creativecommons.org/licenses/by/3.0/&quot;</span> <span style="color: #000066;">-title</span> <span style="color: #009900;">&quot;CC By-Attribution&quot;</span><br />
&nbsp; &nbsp; &nbsp; dc:rights <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Copyright 2010, Some rights reserved (licensed under the Creative Commons Attribution 3.0 Unported license)&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; category <span style="color: #000066;">-scheme</span> <span style="color: #009900;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">-term</span> <span style="color: #009900;">&quot;xml&quot;</span><br />
&nbsp; &nbsp; &nbsp; category <span style="color: #000066;">-scheme</span> <span style="color: #009900;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">-term</span> <span style="color: #009900;">&quot;PowerShell&quot;</span><br />
&nbsp; &nbsp; &nbsp; category <span style="color: #000066;">-scheme</span> <span style="color: #009900;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">-term</span> <span style="color: #009900;">&quot;DSL&quot;</span><br />
&nbsp; &nbsp; &nbsp; fi:folder <span style="color: #000066;">-Path</span> <span style="color: #009900;">&quot;~\Formats&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ChildItem</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Split-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$profile</span><span style="color: #333;">&#41;</span> Formats<span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fi:file <span style="color: #000066;">-Created</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">CreationTimeUtc</span> <span style="color: #000066;">-Modified</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">LastWriteTimeUtc</span> <span style="color: #000066;">-Size</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">Length</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$file</span>.<span style="color: #003366;">Name</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Declaration</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>There are four things you should notice, in particular:</p>

	<p>First: the initial tag has a [XNamespace] added to it. You can specify a tag name that has a namespace by adding them together this way, or by embedding the namespace in the string like <code>&#34;{http://www.w3.org/2005/Atom}feed&#34;</code> instead.  Either way works. This initial namespace becomes the <strong>default</strong> namespace for the document. If you don&#8217;t specify a namespace on tags later, they automatically belong to that one.</p>

	<p>Second: when you want to add additional namespaces, you can do so with a custom prefix like: <code>-dc ([XNamespace]&#34;http://purl.org/dc/elements/1.1&#34;)</code>, and that prefix (dc) takes on a special meaning. When you want to have a tag later on that is part of that namespace, you just prefix the tag, like <code>dc:rights</code> &#8212;the same way you would in <span class="caps">XML</span>.</p>

	<p>Third: any number of attributes can be specified using the <code>-name value</code> syntax, but anything in a <code>{scriptblock}</code> becomes the content &#8212; and is subject to the same rules as the outer sections.</p>

	<p>Fourth: This generates an XDocument. When you cast an XDocument to string, the xml declaration is left off, so if you want it, you need to manually add it via <code>$XDocument.Declaration</code>.  Incidentally, XDocuments are <strong>not</strong> XMLDocuments, but they <strong>are</strong> trivially castable to them.</p>

	<p>The output of that particular section of New-XDocument is this:</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;feed</span> <span style="color: #000066;">xmlns:dc</span>=<span style="color: #ff0000;">&quot;http://purl.org/dc/elements/1.1&quot;</span> <span style="color: #000066;">xmlns:fi</span>=<span style="color: #ff0000;">&quot;http://huddledmasses.org/schemas/FileInfo&quot;</span> <span style="color: #000066;">xml:lang</span>=<span style="color: #ff0000;">&quot;en-US&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2005/Atom&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>http://HuddledMasses.org/<br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;updated<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-03-04T00:44:31Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/updated<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Joel Bennett<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uri<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://HuddledMasses.org/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/uri<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://HuddledMasses.org/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>http://HuddledMasses.org/A-DSL-for-XML-in-PowerShell-New-XDocument/<br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://HuddledMasses.org/A-DSL-for-XML-in-PowerShell-New-XDocument/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;updated<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-03-03T00:00:00Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/updated<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;summary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A while back, I posted a simple mini language for generating XML from PowerShell script. However, I was using it the other day, and I really just felt that the markup was ugly, since it was littered with 'xe' marks and such.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/summary<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000066;">rel</span>=<span style="color: #ff0000;">&quot;license&quot;</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;http://creativecommons.org/licenses/by/3.0/&quot;</span> <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;CC By-Attribution&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dc:rights<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Copyright 2010, Some rights reserved (licensed under the Creative Commons Attribution 3.0 Unported license)<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dc:rights<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">scheme</span>=<span style="color: #ff0000;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">term</span>=<span style="color: #ff0000;">&quot;xml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">scheme</span>=<span style="color: #ff0000;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">term</span>=<span style="color: #ff0000;">&quot;PowerShell&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">scheme</span>=<span style="color: #ff0000;">&quot;http://huddledmasses.org/tag/&quot;</span> <span style="color: #000066;">term</span>=<span style="color: #ff0000;">&quot;DSL&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:folder</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;~\Formats&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:file</span> <span style="color: #000066;">created</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:00Z&quot;</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:00Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;30474&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>CliXml.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:file</span> <span style="color: #000066;">created</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.4529965Z&quot;</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.48001Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;14314&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>format.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:file</span> <span style="color: #000066;">created</span>=<span style="color: #ff0000;">&quot;2009-02-07T13:56:12Z&quot;</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2010-01-16T21:30:06.0562796Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;18275&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>NppExternalLexers.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:file</span> <span style="color: #000066;">created</span>=<span style="color: #ff0000;">&quot;2009-08-09T19:10:06.3647094Z&quot;</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-03-18T21:28:51.6579351Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;5802&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Recommender.Types.Format.ps1xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fi:file</span> <span style="color: #000066;">created</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.4970185Z&quot;</span> <span style="color: #000066;">modified</span>=<span style="color: #ff0000;">&quot;2009-11-07T07:27:40.518029Z&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;5107&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>types.xsd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fi:folder<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/category<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/entry<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>The New-XDocument script itself is on PoshCode in the <a href="http://poshcode.org/1682">Xml Module 4</a> along with a few interesting functions like Select-<span class="caps">XML</span> (which improves over the built-in by being able to ignore namespaces when you write XPath) and Remove-XmlNamespace (which was instrumental in removing namespaces for Select-Xml). There&#8217;s also a  Format-Xml for pretty-printing, and a Convert-Xml for processing <span class="caps">XSL</span> transformations.</p>

	<p>I&#8217;ll probably post some more examples of this in the next week or two, and I really should write some commentary about the function itself, which uses the tokenizer to discover which &#8220;commands&#8221; are really xml nodes &#8230; but for now, I&#8217;ll leave you to enjoy.<h6 class="zemanta-related-title">Related articles by Zemanta</h6><ul class="zemanta-article-ul"><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/json-from-powershell-but-why/"><span class="caps">JSON</span> from PowerShell (but why?)</a> (huddledmasses.org)</li><li class="zemanta-article-ul-li"><a href="http://apache.sys-con.com/node/1211348">What&#8217;s in a Namespace</a> (apache.sys-con.com)</li><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/ignoring-namespaces-in-xpath/">Ignoring Namespaces in XPath</a> (huddledmasses.org)</li></ul></p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/c26e9d24-6176-4c07-a3f7-9eb438404e7e/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=c26e9d24-6176-4c07-a3f7-9eb438404e7e" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/a-dsl-for-xml-in-powershell-new-xdocument/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ignoring Namespaces in XPath</title>
		<link>http://huddledmasses.org/ignoring-namespaces-in-xpath/</link>
		<comments>http://huddledmasses.org/ignoring-namespaces-in-xpath/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 21:32:51 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Xml]]></category>
		<category><![CDATA[XPath]]></category>
		<category><![CDATA[Xsl]]></category>
		<category><![CDATA[Xslt]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1348</guid>
		<description><![CDATA[I know that I just wrote a post last week about XPath and namespaces in PowerShell, but at the time I left out one possible way of dealing with namespaces, because it&#8217;s not the right way of doing things. However, sometimes it&#8217;s nice to have options, and when you&#8217;re working on the command-line in PowerShell, [...]]]></description>
			<content:encoded><![CDATA[	<p>I know that I just wrote a post last week about <a href="http://huddledmasses.org/xpath-and-namespaces-in-powershell/">XPath and namespaces in PowerShell</a>, but at the time I left out one possible way of dealing with namespaces, because it&#8217;s not the <em>right</em> way of doing things.  However, sometimes it&#8217;s nice to have options, and when you&#8217;re working on the command-line in PowerShell, or just trying to figure out a proof-of-concept call to a web service, you really don&#8217;t <em>need</em> to deal with namespaces correctly, you just need it to work.</p>

	<p>With that in mind, I present to you the fourth option: just strip the namespaces out!  The simplest way to do that is to run the <span class="caps">XML</span> through an <span class="caps">XSL</span> stylesheet which just outputs the <code>local-name()</code> of each node (including attributes), and remove any namespace definitions (processing instructions).</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">indent</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/|comment()|processing-instruction()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;{local-name()}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:apply-templates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:element<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;@*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;{local-name()}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:value-of<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:output<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>That stylesheet and the basic steps of the process will work anywhere, from Java to C# to the web &#8230; but since my current language of choice for prototyping is PowerShell, I&#8217;ll show you how to implement it there as <a href="http://poshcode.org/1492">Remove-XmlNamespace</a>. Once you have that, I think you&#8217;ll see that it was relatively simple for me to write <a href="http://poshcode.org/1504">a new Select-XML</a> which adds a parameter <code>RemoveNamespace</code> which is implemented by calling this <a href="http://poshcode.org/1492">Remove-XmlNamespace</a> &#8230;</p>

	<p>That actually allows you to call <code>Select-Xml</code> with the <code>-RemoveNamespace</code> parameter just as though the namespaces didn&#8217;t exist.  Of course, the returned <span class="caps">XML</span> nodes will, in fact, <span class="caps">NOT</span> have namespaces &#8230; so they may not be quite the same as the source, but the data will all be there.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':-)' class='wp-smiley' /> </p>

<span id="more-1348"></span>

<script type="text/javascript" src="http://PoshCode.org/embed/1504"></script>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/bba0ce48-3de7-4675-8c1a-a449e3ae8a96/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=bba0ce48-3de7-4675-8c1a-a449e3ae8a96" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/ignoring-namespaces-in-xpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XPath and Namespaces in PowerShell</title>
		<link>http://huddledmasses.org/xpath-and-namespaces-in-powershell/</link>
		<comments>http://huddledmasses.org/xpath-and-namespaces-in-powershell/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 15:44:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Namespace]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Xml]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1326</guid>
		<description><![CDATA[Although there are a few &#8220;CSS Selector&#8221; libraries, most browsers haven&#8217;t even implemented CSS3 selectors, never mind frameworks like .Net or scripting languages like Javascript or PowerShell so XPath remains the most powerful way to deal with finding specific data in an XML file, and by extension, XHTML and even HTML files (if you can [...]]]></description>
			<content:encoded><![CDATA[	<p>Although there are a few &#8220;<span class="caps">CSS</span> Selector&#8221; libraries, most <em>browsers</em> haven&#8217;t even implemented CSS3 selectors, never mind frameworks like .Net or scripting languages like Javascript or PowerShell  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  so XPath remains the most powerful way to deal with finding specific data in an <span class="caps">XML</span> file, and by extension, <span class="caps">XHTML</span> and even <span class="caps">HTML</span> files (if you can convert them using something like <a href="http://code.msdn.microsoft.com/SgmlReader">SgmlReader</a>) is to use XPath queries.</p>

	<p>There are a lot of <a href="http://www.w3schools.com/xpath/">XPath tutorials</a> around the web, so there&#8217;s no need for me to get into that very much, but I just wanted to write a brief note about using XPath with documents that have namespaces (particularly, from .Net).  The problem is that in order to select nodes that have a namespace assigned, you <strong>must</strong> use a namespace prefix and a <code>NamespaceManager</code>. So even if it&#8217;s the default namespace, if there&#8217;s an xmlns=&#8221;...&#8221; on the document, you have to create and use a prefix.</p>

	<p>The bottom line is this: if you have an <span class="caps">XML</span> document that looks like this:</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;event</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eventid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eventid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/level<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;task<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4002<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/task<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>34<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eventdata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootTsVersion&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootStartTime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2009-11-22T08:45:11.640400200Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootEndTime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2009-11-22T08:48:23.432178500Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SystemBootInstance&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;UserBootInstance&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>15<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eventdata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/event<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>That bit at the top where it says: <code>xmlns=&#34;http://schemas.microsoft.com/win/2004/08/events/event&#34;</code> is assigning a <strong>default</strong> namespace. Sometimes you&#8217;ll see something like this (eg: in <span class="caps">RSS</span>):</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> <span style="color: #000066;">xmlns:media</span>=<span style="color: #ff0000;">&quot;http://search.yahoo.com/mrss/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Buttons<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>http://www.flickr.com/photos/jaykul/4114286262/<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Quick cut-n-paste from a screenshot<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubdate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tue, 17 Nov 2009 20:46:31 -0800<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubdate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;guid</span> <span style="color: #000066;">ispermalink</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>tag:flickr.com,2004:/photo/4114286262<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:content</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://farm3.static.flickr.com/2552/4114286262_001e2f8905_o.png&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;image/jpeg&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;157&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;337&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Buttons<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:description</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;lt;</span>p<span style="color: #ddbb00;">&amp;gt;</span>Quick cut-n-paste from a screenshot into your mockup<span style="color: #ddbb00;">&amp;lt;</span>/p<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:thumbnail</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://farm3.static.flickr.com/2552/4114286262_cba8aaf967_s.jpg&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;75&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;75&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:credit</span> <span style="color: #000066;">role</span>=<span style="color: #ff0000;">&quot;photographer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Jaykul<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:credit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:category</span> <span style="color: #000066;">scheme</span>=<span style="color: #ff0000;">&quot;urn:flickr:tags&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>screenshot snagit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:thumbnail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:content<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/channel<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/rss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>This one assigns a specific prefix &#8220;media&#8221; to the namespace url &#8220;http://search.yahoo.com/mrss/&#8221; ...</p>

	<h3>Selecting nodes with namespaces</h3>

	<p>In either case, if you want to select a node that&#8217;s assigned to a namespace (which is <span class="caps">ALL</span> the nodes in the first example, but just the ones that start with media: in the second example) in .net, you have to specify the namespace in order to select those nodes with XPath.  PowerShell 2.0 has a <code>Select-Xml</code> cmdlet which accepts <code>-Namespaces</code> as a parameter: you simply provide a hashtable of names to urls.</p>

	<p>If you had loaded the first document above into <code>$xml</code>, you could select the BootStartTime and BootEndTime using an XPath query like this: <code>//e:Data[</code>Name = &#8216;BootStartTime&#8217; or <code>Name = &#39;BootEndTime&#39;]</code> but we have to <span class="caps">DEFINE</span> that &#8220;e&#8221; namespace.  To do so using <code>Select-Xml</code> you just pass it into the command</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:Data[@Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:Data[@Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span> e <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-Xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #000066;">-XPath</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span> <span style="color: #000066;">-Namespace</span> <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Select</span> <span style="color: #000066;">-Expand</span> Node <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-expand</span> Value<br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-Xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #000066;">-XPath</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span> <span style="color: #000066;">-Namespace</span> <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Select</span> <span style="color: #000066;">-Expand</span> Node <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-expand</span> Value<br />
<br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<p>Of course, you don&#8217;t have to use Select-Xml, you can do this in plain .Net without cmdlets (and this is what you would <strong>have</strong> to do in C#). In fact, depending on the situation, it might even be simpler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:data[@name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:data[@name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> <span style="color: #003366; font-weight: bold;">Xml</span>.<span style="color: #003366;">XmlNamespaceManager</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">NameTable</span><br />
<span style="color: #660033; font-weight: bold;">$ns</span>.<span style="color: #003366;">AddNamespace</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;e&quot;</span>, <span style="color: #009900;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span> <span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span>, <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span>, <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<h3>Selecting nodes regardless of namespaces</h3>

	<p>There are, however, two ways that you could avoid specifying the namespaces.  The first is to just avoid specifying the node name at all. In that first example, that would be fairly easy, because the &#8220;BootStartTime&#8221; and &#8220;BootEndTime&#8221; names are unique to the nodes we&#8217;re interested in (even if there were boatloads of identical events, you&#8217;ll never have a Name=&#8220;BootStartTime&#8221; attribute on the <system> tag, for instance).  The second way is to filter using the local-name function, and specify just the &#8220;local&#8221; name (which is the non-namespace-qualified part of the name).</p>

	<p>So to ignore the tag name, we can just use a * for a wildcard.  The only real difference is that we don&#8217;t need the namespace, and the XPath pattern will be different:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[@Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[@Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Node</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Node</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<p>Or to specify the local name, we can use the Local-Name function.  Again, we don&#8217;t need a namespace, we are just changing the XPath to be a more specific match:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[local-name() = 'Data' and @Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[local-name() = 'Data' and @Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div></system></p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/1b73f9a4-bb71-44bc-844b-1302b5a0151a/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=1b73f9a4-bb71-44bc-844b-1302b5a0151a" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/xpath-and-namespaces-in-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Get-Web (Another round of wget for PowerShell)</title>
		<link>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/</link>
		<comments>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/#comments</comments>
		<pubDate>Fri, 02 May 2008 02:08:08 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Get-Web]]></category>
		<category><![CDATA[Http]]></category>
		<category><![CDATA[HttpWebRequest]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[wGet]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=520</guid>
		<description><![CDATA[Well, after multiple attempts at a wget PowerShell script (the last one works very well for downloading web pages, files, as long as you don&#8217;t need to send post parameters or anything like that) ... I found myself writing a script last week that included a custom HTTP POST function as well as using some [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, after multiple attempts at a <a href="http://huddledmasses.org/wget-2-for-powershell/">wget PowerShell script</a> (the last one works very well for downloading web pages, files, as long as you don&#8217;t need to send post parameters or anything like that) ... I found myself writing a script last week that included a custom <span class="caps">HTTP</span> <span class="caps">POST</span> function as well as using some prior functionality I wrote (<code>ConvertFrom-Html</code>) to convert <span class="caps">HTML</span> files to <span class="caps">XML</span> documents &#8212; which PowerShell can deal with nicely.</p>

	<p>So I&#8217;ve taken my <a href="http://huddledmasses.org/converting-html-to-xml-in-powershell/">Huddled.Html</a> code and worked it in together with a few extra bits &#8230; and I&#8217;m releasing it under the new name &#8220;PoshHttp&#8221; (which has the sole benefit of being short, in the case when you need to type the full command names, like: <code>PoshHttp\Get-Web</code>.</p>

	<p>In lieu of providing proper documentation for this, there&#8217;s a script below which shows off some of the features of the <code>Get-Web</code> cmdlet &#8230; but first, you&#8217;re going to want to <a href="/downloads/PoshHttp.7z">download PoshHttp</a> and I can give you a basic overview.</p>

	<h4>Get-Web </h4>

	<p>Downloads a file from the web <span class="caps">URI</span>, optionally passing <span class="caps">POST</span> or <span class="caps">GET</span> parameters which it accepts as a Hashtable. If the resulting file is an xml or html file, it converts it to <span class="caps">XML</span> and outputs it as an <code>XmlDocument</code> object. If they are not, it saves them to file, and outputs the <code>FileInfo</code> object. </p>

	<ul>
		<li><strong>Plain</strong> will prevent the conversion to xml</li>
		<li><strong>Save</strong> will force saving to file (either xml or binaries)</li>
		<li><strong>Path</strong> lets you specify the file name/path (normally, it selects a path based on what you&#8217;re downloading).</li>
	</ul>
	<ul>
		<li><strong>Force</strong> will prevent it from asking if you want to overwrite files</li>
	</ul>

	<h4>ConvertFrom-Html</h4>

	<p>Converts <span class="caps">HTML</span> to <span class="caps">XML</span>. I&#8217;ll eventually tweak this so it supports passing it a file path and even saving the xml in-place on the original file &#8230; but for now you can use it on a stream of text, like this: <code>(Get-Content file.html | ConvertFrom-Html).save( &#34;file.xml&#34; )</code>.  Note that it outputs an xmldocument object &#8230; you can get the xml as text to the console using <code>Get_OuterXml()</code>.</p>

	<h4> ConvertTo-Hashtable</h4>

	<p>Converts a url-style query string or NameValueCollection into a Hashtable. It&#8217;s mainly to make it easier to use the Get-Web in cases where you&#8217;re getting the <span class="caps">URL</span> or <span class="caps">POST</span> data from elsewhere.</p>

	<p>Here&#8217;s a script with a few example uses of Get-Web: <span id="more-520"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Find the url of the image (download an html file as an xmldocument)</span><br />
<span style="color: #660033; font-weight: bold;">$ImgURL</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>fast<span style="color: #66cc66;">/</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">html</span>.<span style="color: #003366;">body</span>.<span style="color: #003366;">div</span>.<span style="color: #003366;">img</span>.<span style="color: #003366;">src</span><br />
<br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">Name</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;PoshConsole&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#In PoshConsole, show it inline</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ImgUrl</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">WPF</span></span> <span style="color: #000066;">-source</span> <span style="color: #009900;">&quot;&lt;Image Height='174' Source='{Binding Path=AbsoluteUri}' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' /&gt;&quot;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Use Get-Web to download an image (it's saved to file by default), and open it in my default viewer</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## When Get-Web saves a file, it outputs the [FileInfo] which can be invoked with &amp;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://dilbert.com$ImgURL&quot;</span> <span style="color: #000066;">-force</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Determine the language of a snippet of text (5 words minimum for best results)</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Language</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This time, we're using Get-Web with POST parameters ...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://www.xrce.xerox.com/cgi-bin/mltt/LanguageGuesser&quot;</span> <span style="color: #000066;">-Post</span> <span style="color: #333;">&#40;</span>@<span style="color: #333;">&#123;</span>Text<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;//font&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #009900;">&quot;#text&quot;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">###################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Translate text to English ... This is obviously reworkable as a general translation tool</span><br />
<span style="color: #666666; font-style: italic;">## But I don't have much use for that, since I only speak Spanish, English, and code ...</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">English</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$text</span>,<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$FromLanguage</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">Language</span></span> <span style="color: #660033; font-weight: bold;">$text</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$post</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span>text<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$text</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$FromLanguage</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Arabic&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ar|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Chinese&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;zh|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Dutch&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;nl|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;French&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;fr|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;German&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;de|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Greek&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;el|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Italian&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;it|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Japanese&quot;</span> &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ja|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Korean&quot;</span> &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ko|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Portuguese&quot;</span> &nbsp; <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;pt|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Russian&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;ru|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;Spanish&quot;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$post</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;langpair&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;es|en&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #009900;">&quot;Sorry, but I can't translate $FromLanguage&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Using Get-Web with POST parameters ... nothing remarkable, but it's just nice with the previous function ...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://www.google.com/translate_t&quot;</span> <span style="color: #000066;">-Post</span> <span style="color: #660033; font-weight: bold;">$post</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;//div[@id='result_box']&quot;</span><span style="color: #333;">&#41;</span>.<span style="color: #009900;">&quot;#text&quot;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">####################################</span><br />
<span style="color: #666666; font-style: italic;">## Figure out the real url behind those shortened forms</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$urls</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$snip</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?(?:snurl|snipr|snipurl)\.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$tiny</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?TinyURL.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$isgd</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?is.gd/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$twurl</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?twurl.nl/([^?/ ]*)\b&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">switch</span> <span style="color: #000066;">-regex</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$urls</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$snip</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Notice that {{ Get-Web ... -plain }} returns plain text, not an xml doc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$snip</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://snipurl.com/resolveurl?id=$($snip.match( $_ ).groups[1].value)&quot;</span> <span style="color: #000066;">-plain</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$tiny</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## GET parameters this time ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://tinyurl.com/preview.php&quot;</span> <span style="color: #000066;">-Get</span> @<span style="color: #333;">&#123;</span>num<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$tiny</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">match</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">groups</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">value</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$tiny</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.SelectSingleNode(&quot;</span><span style="color: #009900;">&quot;//a[@id='redirecturl']&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$isgd</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://is.gd/$($isgd.match( $_ ).groups[1].value)-&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$isgd</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.SelectSingleNode(&quot;</span><span style="color: #009900;">&quot;//div[@id='main']/p/a&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$twurl</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$doc</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Web</span></span> <span style="color: #009900;">&quot;http://tweetburner.com/links/$($twurl.match( $_ ).groups[1].value)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$twurl</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Replace</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>, <span style="color: #009900;">&quot;$($doc.selectsingleNode(&quot;</span><span style="color: #009900;">&quot;//div[@id='main-content']/p/a&quot;</span><span style="color: #009900;">&quot;).href)&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/get-web-another-round-of-wget-for-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert-Xml with XSLT in PowerShell</title>
		<link>http://huddledmasses.org/convert-xml-with-xslt-in-powershell/</link>
		<comments>http://huddledmasses.org/convert-xml-with-xslt-in-powershell/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 04:54:04 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Xml]]></category>
		<category><![CDATA[Xsl]]></category>
		<category><![CDATA[Xslt]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/convert-xml-with-xslt-in-powershell/</guid>
		<description><![CDATA[Just a quick post to upload a simple PowerShell script that transforms XML files with XSL files. There&#8217;s actually a cmdlet in PowerShell Community Extensions which does this, but believe it or not, in all my tests the script outperforms the cmdlet (the cmdlet takes, on average, 117ms on a file which takes the script [...]]]></description>
			<content:encoded><![CDATA[	<p>Just a quick post to upload a simple PowerShell script that transforms <span class="caps">XML</span> files with <span class="caps">XSL</span> files.  There&#8217;s actually a cmdlet in <a href="http://CodePlex.com/PowerShellCX/">PowerShell Community Extensions</a> which does this, but believe it or not, in all my tests the script outperforms the cmdlet (the cmdlet takes, on average, 117ms on a file which takes the script 63ms on average).</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Convert-<span style="font-style: normal;">WithXslt</span></span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span>, <span style="color: #660033; font-weight: bold;">$xslFilePath</span>, <span style="color: #660033; font-weight: bold;">$outputFilePath</span><span style="color: #333;">&#41;</span> <br />
<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Simplistic error handling</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$xslFilePath</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">resolve-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$xslFilePath</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #000066;">-not</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$xslFilePath</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;Can't find the XSL file&quot;</span> <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">resolve-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #000066;">-not</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;Can't find the XML file&quot;</span> <span style="color: #333;">&#125;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$outputFilePath</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">resolve-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$outputFilePath</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #000066;">-not</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">test-<span style="font-style: normal;">path</span></span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">split-<span style="font-style: normal;">path</span></span> <span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;Can't find the output folder&quot;</span> <span style="color: #333;">&#125;</span> <br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Get an XSL Transform object (try for the new .Net 3.5 version first)</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$EAP</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ErrorActionPreference</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$ErrorActionPreference</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;SilentlyContinue&quot;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$script</span>:xslt <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366; font-weight: bold;">xml</span>.<span style="color: #003366;">xsl</span>.<span style="color: #003366;">xslcompiledtransform</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">trap</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSArgumentException</span><span style="color: #333;">&#93;</span></span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#123;</span> &nbsp;<span style="color: #666666; font-style: italic;"># no 3.5, use the slower 2.0 one</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$ErrorActionPreference</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$EAP</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$script</span>:xslt <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366; font-weight: bold;">xml</span>.<span style="color: #003366;">xsl</span>.<span style="color: #003366;">xsltransform</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$ErrorActionPreference</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$EAP</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## load xslt file</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$xslt</span>.<span style="color: #003366;">load</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xslFilePath</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## transform </span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$xslt</span>.<span style="color: #003366;">Transform</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$originalXmlFilePath</span>, <span style="color: #660033; font-weight: bold;">$outputFilePath</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/convert-xml-with-xslt-in-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Converting HTML to XML in PowerShell</title>
		<link>http://huddledmasses.org/converting-html-to-xml-in-powershell/</link>
		<comments>http://huddledmasses.org/converting-html-to-xml-in-powershell/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 21:40:32 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Cmdlet]]></category>
		<category><![CDATA[Converting]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Parsing]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Xml]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/converting-html-to-xml-in-powershell/</guid>
		<description><![CDATA[I&#8217;ll write up more information later, but a couple people have asked for this in #PowerShell on irc.freenode.net, and I had it already written, so here you go &#8230; my ConvertFrom-Html cmdlet (in a Huddled.HtmlSnapin). It converts HTML to valid xml using the SGML Parser which was available on GotDotNet years ago. It only works [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ll write up more information later, but a couple people have asked for this in #PowerShell on irc.freenode.net, and I had it already written, so here you go &#8230; my ConvertFrom-Html cmdlet (in a Huddled.HtmlSnapin). It converts <span class="caps">HTML</span> to valid xml using the <span class="caps">SGML</span> Parser which was available on GotDotNet years ago. It only works with files (doesn&#8217;t do <span class="caps">URL</span> downloads yet). Use it like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://huddledmasses.org/&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Join-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$pwd</span> <span style="color: #009900;">&quot;HuddledMasses.html&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$client</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Net</span>.<span style="color: #003366;">WebClient</span><br />
<span style="color: #660033; font-weight: bold;">$client</span>.<span style="color: #003366;">DownloadFile</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$url</span>, <span style="color: #660033; font-weight: bold;">$file</span> <span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;">#NOTE: You need to use a full path here, not relative</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">ConvertFrom-<span style="font-style: normal;">Html</span></span> <span style="color: #660033; font-weight: bold;">$file</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Or even</span><br />
<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">ConvertFrom-<span style="font-style: normal;">Html</span></span> <span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Save</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$file</span><span style="color: #333;">&#41;</span></div>

	<p>The source code to my plugin may be considered public domain, and is included in <a href="http://HuddledMasses.org/wordpress/wp-content/uploads/2007/11/huddledhtmlsnapin.zip">the Huddled <span class="caps">HTML</span> SnapIn Zip</a>.  </p>

	<p><del>However, the SgmlReader library is a Microsoft Sample which is licensed under <a href="http://dev.live.com/sampleseula.aspx">the old MS Samples license</a> which doesn&#8217;t allow reuse with viral open source software. I&#8217;ve seen some work being done on an <a href="http://www.codeplex.com/htmlagilitypack">HtmlAgilityPack</a> on CodePlex (using a Creative Commons <a href="http://www.codeplex.com/htmlagilitypack/Project/License.aspx"><span class="caps">ASA</span> license</a>) but I have not really looked at it except to see that it has a several active issues related to entity encoding and dropping malformed tags which I haven&#8217;t encountered in SgmlReader &#8230;</del></p>

	<p>The <a href="http://code.msdn.microsoft.com/SgmlReader">SGMLReader</a> library has been re-released on <a href="http://code.msdn.microsoft.com/"><span class="caps">MSDN</span> Code Gallery</a> under an Ms-PL license, and all is well with the world.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':D' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/converting-html-to-xml-in-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

