Repository navigation
Description
To enable colored output in output formats, I can write a Format.ps1xml file that uses ANSI color codes like so:
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<ViewDefinitions>
<!-- Resembles the git log output with format=medium (default) -->
<View>
<Name>Medium</Name>
<ViewSelectedBy>
<TypeName>PowerGit.CommitInfo</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<!-- Yellow -->
<Label>[33mSha</Label>
<ScriptBlock>"$($_.Sha)$([char]0x001b)[0m"</ScriptBlock>
</ListItem>
<ListItem>
<Label>Author</Label>
<PropertyName>Author</PropertyName>
</ListItem>
<ListItem>
<Label>Date</Label>
<ScriptBlock>"$($_.Author.When)"</ScriptBlock>
</ListItem>
<ListItem>
<Label>Message</Label>
<PropertyName>Message</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>Note the [33m, which is ESC[33m, which is the ANSI code for yellow, and $([char]0x001b)[0m, which is ESC[0m, which is the ANSI reset code.
Together, this will color the whole line yellow:
just like in git log output:
HOWEVER this is actually not valid XML. Every good XML parser will rightfully complain:
See https://en.wikipedia.org/wiki/Valid_characters_in_XML#XML_1.0
Which means these format files cannot be statically checked, generated, codemodded, etc.
It is valid in XML 1.1: https://en.wikipedia.org/wiki/Valid_characters_in_XML#XML_1.1
But if I change the parsing instruction to
<?xml version="1.1" encoding="utf-8"?>PowerShell errors:
Import-Module : Errors occurred while loading the format data file:
/Users/felix/src/github.com/felixfbecker/PowerGit/PowerGit/Formats/PowerGit.CommitInfo.formats.ps1xml, Error in file /Users/felix/src/github.com/felixfbecker/PowerGit/PowerGit/Formats/PowerGit.CommitInfo.formats.ps1xml: Version number '1.1' is invalid. Line 1, position 16.
At /Users/felix/src/github.com/felixfbecker/PowerGit/PowerGit/Import-PowerGit.ps1:32 char:1
+ Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'PowerG ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Import-Module], RuntimeException
+ FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommandIt effectively already parses XML 1.1 fine though, it just complains about the version.



Activity
felixfbecker commentedon Oct 18, 2018
Steve Lee (@SteveL-MSFT) responding to #7749 (comment)
It seems like the parser is parsing XML 1.1 just fine though, it just rejects the 1.1 processing instruction. Could we just regex replace the version in the processing instruction before handing the string to the parser? It’s just the first line and not full XML, so should be safe to transform with a regex.
SteveL-MSFT commentedon Oct 19, 2018
Felix Becker (@felixfbecker) a bit hacky, but it might be acceptable. Need someone more of an expert with XML to indicate if there are any unanticipated side effects
JamesWTruher commentedon Jun 1, 2021
it seems like 1.1 added new capabilities which PowerShell may not be able to understand. At this point, we think it should remain xml version 1.0.