【问题标题】:xdt transform fails on HTML file on P tag在 P 标签上的 HTML 文件上,xdt 转换失败
【发布时间】:2012-07-02 10:11:37
【问题描述】:

我正在尝试对 HTML 文件应用转换。转换适用于 div 和 span,但一旦我插入 P 标签,它就会失败。

我正在转换的 HTML 文件是:

<!DOCTYPE html>
<html>
  <head>
    <title>STREAM Interactive - UAT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  </head>
    <body>
          <p>my para</p>
    </body>
</html>

转换文件很简单:

<html xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <head>
     <title xdt:Transform="Replace">TITLEHEADINGHERE</title>
   </head>  

</html>

用于进行转换的项目文件是:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml" AssemblyFile="C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.Dll"/>

    <PropertyGroup>
        <SourceFile>web.config</SourceFile>
        <TransformFile>web.debug.config</TransformFile>
        <OutputFile>final.config</OutputFile>
    </PropertyGroup>

    <Target Name="GenerateConfigs">
        <Message Text="Beginning Transformation." />
        <Message Text="$(SourceFile)" />
        <TransformXml Source="$(SourceFile)" Transform="$(TransformFile)" Destination="$(OutputFile)" />
        <Message Text="Completed Transformation." />
    </Target>

</Project>

使用的命令是:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe D:\SAM\SAMSite\bin\msbuild-transform-Template.xml /p:SourceFile="C:\temp\buildTest\BlueZone\2.3.10.5\PublishedClickOnce\index.html" /p:TransformFile="D:\SAM\SAMSite\bin\TransformTemplates\clickonce.index.transform.temp" /p:OutputFile="C:\temp\buildTest\BlueZone\2.3.10.5\PublishedClickOnce\index.html.temp"

我得到的输出/错误是:

Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.269]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 02/07/2012 10:54:33.
Project "D:\SAM\SAMSite\bin\msbuild-    transform-Template.xml" on node 1 (default targets).
GenerateConfigs:
  Beginning Transformation.
  C:\temp\buildTest\BlueZone\2.3.10.5\PublishedClickOnce\index.html
  Transforming Source File:     C:\temp\buildTest\BlueZone\2.3.10.5\PublishedClickOnce\index.html
  Transformation succeeded
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018: The "TransformXml" task failed unexpectedly.
D:\SAM\SAMSite\bin\msbuild-transform-    Template.xml(13,3): error MSB4018: System.UriFormatException: Invalid URI: The URI is empty.
D:\SAM\SAMSite\bin\msbuild-transform-    Template.xml(13,3): error MSB4018:    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Web.Publishing.Tasks.TransformXml.Execute()
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)
Done Building Project "D:\SAM\SAMSite\bin\msbuild-transform-Template.xml" (default targets) -- FAILED.

Build FAILED.

"D:\SAM\SAMSite\bin\msbuild-transform-Template.xml" (default target) (1) ->
(GenerateConfigs target) -> 
  D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018: The "TransformXml" task failed unexpectedly.
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018: System.UriFormatException: Invalid URI: The URI is empty.
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Web.Publishing.Tasks.TransformXml.Execute()
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
D:\SAM\SAMSite\bin\msbuild-transform-Template.xml(13,3): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.09

HTML 已经被精简到最简单的程度,以前那里有 div 和 span。有趣的是,如果我给 P 标签一个类,它就可以工作。

我不知道为什么会这样。

编辑

根据@Kieren 的建议,我尝试直接使用 TransformXml 运行。我收到一个新错误:

Data at the root level is invalid. Line 1, position 1.

   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at Microsoft.Web.Publishing.Tasks.XmlAttributePreservationDict.ReadPreservationInfo(String elementStartTag)
   at Microsoft.Web.Publishing.Tasks.XmlAttributePreservationProvider.GetDictAtPosition(Int32 lineNumber, Int32 linePosition)
   at Microsoft.Web.Publishing.Tasks.XmlFileInfoDocument.XmlFileInfoElement..ctor(String prefix, String localName, String namespaceUri, XmlFileInfoDocument document)
   at Microsoft.Web.Publishing.Tasks.XmlFileInfoDocument.CreateElement(String prefix, String localName, String namespaceURI)
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
   at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at Microsoft.Web.Publishing.Tasks.XmlFileInfoDocument.LoadFromTextReader(TextReader textReader)
   at Microsoft.Web.Publishing.Tasks.XmlFileInfoDocument.LoadFromFileName(String     filename)
   at Microsoft.Web.Publishing.Tasks.XmlFileInfoDocument.Load(String filename)
   at QuickTests.TestTransformXml.OpenSourceFile(String sourceFile) in C:\Users\jon\Documents\Visual Studio 2010\Projects\QuickTests\QuickTests\TestTransformXmlTask.cs:line 140

【问题讨论】:

  • 只是为了确认路径都正确,因为我可以从文件中删除 P 并运行相同的命令,一切正常。
  • 用html5.validator.nu仔细检查了HTML是否有效,(感觉它可能是命名空间或其他东西,想确保它至少是正确的html)。
  • 查看 Microsoft.Web.Publishing.Tasks.dll 的源代码:必须抛出 XmlException;然后执行此代码:Uri uri = new Uri(ex.SourceUri);,但正如 XmlException 文档所示,SourceUri 可以是 null。微软在他们的错误日志中的错误意味着这个异常很难追踪!也许 Reflector VS 插件会让你附加到 msbuild.exe,逐步调试 Microsoft.Web.Publishing.Tasks.TransformXml 方法。
  • 您也可以根据反射代码自己重新创建TransformXml 类:它看起来很简单并且使用Microsoft.Web.Publishing.Tasks.XmlTransformation 对象,这是一个您可以引用和调用自己的公共类

标签: html msbuild transform xdt-transform


【解决方案1】:

根据我提交的错误报告,此问题现已修复。假设它将包含在 MsBuild 的下一个版本中,(不知道什么时候):

https://connect.microsoft.com/VisualStudio/feedback/details/752279/msbuild-tansform-failure-for-elements-with-only-one-character

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多