如果导入不存在,XSLT 当前将插入导入
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
...
<Import Project="$(SolutionDir)BuildShared.targets" />
</Project>
我需要它把它作为第一个节点插入
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(SolutionDir)BuildShared.targets" />
...
...
</Project>
模板.xsl;
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match='@*|node()'>
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
交换 import 和 apply-templates 行给出;
运行时错误:文件 template.xsl 第 9 行元素复制
属性节点必须添加到元素的任何子节点之前。
最佳答案
只需分别为 node()
和 @*
执行您的 xsl:apply-templates
:
<xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
关于xml - 使用 XSL 在第一个位置插入 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10926518/