Skip to content

Allow .ps1xml files in XML 1.1 #7526

@felixfbecker

Description

@felixfbecker

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>&#27;[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 &#27;[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:

image

just like in git log output:

image

HOWEVER this is actually not valid XML. Every good XML parser will rightfully complain:

image

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.ImportModuleCommand

It effectively already parses XML 1.1 fine though, it just complains about the version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-By DesignThe reported behavior is by design.WG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions