2012-06-07 2 views
4

XSLT будет в настоящее время вставить импорт, если он не существуетВставьте узел XML в первом положении с помощью XSL

<?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> 

Взаимозаменяемые строки импорта и применения шаблонов;

runtime error: file template.xsl line 9 element copy

Attribute nodes must be added before any child nodes to an element.

ответ

4

Просто сделайте ваш xsl:apply-templates для node() и @* отдельно:

<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> 
Смежные вопросы