Skip to content

Allow .ps1xml files in XML 1.1 #7526

Closed
Closed

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>&#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.

Activity

  1. added
    Issue-Enhancement
    on Aug 16, 2018
  2. felixfbecker commented on Oct 18, 2018

    @felixfbecker
    ContributorAuthor

    Steve Lee (@SteveL-MSFT) responding to #7749 (comment)

    unfortunately, the best I can tell, the .NET XmlDocument type only supports XML 1.0.

    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.

  3. SteveL-MSFT commented on Oct 19, 2018

    @SteveL-MSFT
    Member

    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

  4. JamesWTruher commented on Jun 1, 2021

    @JamesWTruher
    Collaborator

    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.

  5. added
    Resolution-By Design
    on Jun 1, 2021
  6. ghost closed this as completedon Jul 8, 2021
  7. added
    WG-Cmdlets
    and removed on Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementResolution-By DesignWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions