XmlPeek 任务不允许执行附加的 Xslt 函数。不幸的是,msbuild 中没有内置的字符串函数。
我有两种解决方案:一种使用 amn XslTranformation,另一种使用 Exec 的命令。
XslTransform
您可以使用XslTransformation 任务获取您的构建文件并利用substring-before 和substring-after xslt 函数对其进行转换。输出存储在一个文件中(在我的例子中是 test-xslt.out),并使用 ReadLinesfromFile 读取并转换为 Items。
有一个警告:在这种情况下,变量不会被替换,而对于 XmlPeek,它们会被替换。
safexml 编码版本如下所示:
<XslTransformation XmlInputPaths = "test-xslttrans.csproj"
XslContent="
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003"
>
<xsl:output method="text" indent="yes" />
<xsl:template match="text()|@*"/>
<xsl:template match="/msb:Project/msb:PropertyGroup[contains(@Condition,'Debug')]">
<xsl:value-of select="substring-before(@Condition,'==')"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="substring-after(@Condition,'==')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet> "
OutputPaths = "test-xslt.out">
</XslTransformation>
<ReadLinesFromFile
File="test-xslt.out" >
<Output
TaskParameter="Lines"
ItemName="SplittedPeek"/>
</ReadLinesFromFile>
<Message Text="%(SplittedPeek.Identity)" />
为方便起见,这里是普通的 xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msb="http://schemas.microsoft.com/developer/msbuild/2003">
<xsl:output method="text" indent="yes" />
<xsl:template match="text()|@*"/>
<xsl:template match="/msb:Project/msb:PropertyGroup[contains(@Condition,'Debug')]" >
<xsl:value-of select="substring-before(@Condition,'==')"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="substring-after(@Condition,'==')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
命令执行
此解决方案将您的 Peeked 值写入文件。通过调用命令处理器的 FOR /F 命令,我们可以解析行并将它们分割在一些非常适合这里的严格边界内。 EXEC 的输出被写入文件 split.txt。 ReadLinesFromFiles 用于读取 Item 中的值。
<WriteLinesToFile
File="test-xslt-write.out"
Lines="@(Peeked)"
Overwrite="true"
Encoding="ASCII"/>
<Exec Command="for /f "tokens=1* delims==" %%a in (test-xslt-write.out) do @echo %%a > split.txt"/>
<Exec Command="for /f "tokens=1* delims==" %%a in (test-xslt-write.out) do @echo %%b >> split.txt"/>
<ReadLinesFromFile
File="test-xslt.out" >
<Output
TaskParameter="Lines"
ItemName="SplittedCmdPeek"/>
</ReadLinesFromFile>
<Message Text="%(SplittedCmdPeek.Identity)" />