我可以使用XSL获取MSBuild项目中包含的所有文件的列表吗?

时间:2012-11-06 11:09:49

标签: xslt msbuild

确定。我有一个application.proj文件 在它看起来像... ...

<?xml version="1.0"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="PreBuild;Build;PostBuild;Deploy;PostDeploy;Test" >

    <PropertyGroup>
        <ApplicationName>application</ApplicationName>
        <SolutionPath>C:\Projects\application\Solution\application.sln</SolutionPath>
    </PropertyGroup>

    <Import Project="$(SolutionPath)\SharedBuild.properties"/>
    <Import Project="$(ApplicationName).properties"/>
</Project>

每个包含的项目也可以包括无限项目。 所以我想要一些深入挖掘并获得每个文件的实际路径的方法。

我可以在XSL中执行此操作,还是需要恢复为C#或其他内容?

感谢。

1 个答案:

答案 0 :(得分:1)

的ROOT.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Project>  
  <PropertyGroup>
    <ApplicationName>application</ApplicationName>
  </PropertyGroup>

  <Import Project="project1.xml"/>
  <Import Project="project2.xml"/>
</Project>

project1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Project>  
  <PropertyGroup>
    <ApplicationName>project1</ApplicationName>
  </PropertyGroup>
  <Import Project="project3.xml"/>
</Project>

project2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Project>  
  <PropertyGroup>
    <ApplicationName>project2</ApplicationName>
  </PropertyGroup>
</Project>

project3.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Project>  
  <PropertyGroup>
    <ApplicationName>project3</ApplicationName>
  </PropertyGroup>
</Project>

那么这可能是某种事情?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="2.0">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <projects>
      <!--<xsl:sequence select="test:project(document-uri(.), /Project)" />-->
      <xsl:apply-templates select="/Project">
        <xsl:with-param name="cur-path" select="document-uri(.)" />
      </xsl:apply-templates>
    </projects>
  </xsl:template>

  <xsl:template match="Project">
    <xsl:param name="cur-path" as="xs:anyURI" />
    <project path="{$cur-path}">
      <name><xsl:value-of select="//ApplicationName" /></name>
    </project>
    <xsl:for-each select="//Import">
      <xsl:variable name="path" select="resolve-uri(@Project, $cur-path)" />
      <xsl:apply-templates select="document(@Project)/Project">
        <xsl:with-param name="cur-path" select="$path" />
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

针对root.xml运行会产生:

<?xml version="1.0" encoding="UTF-8"?>
<projects xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="urn:test">
   <project path="file:/C:/Dropbox/Public/xsl/recursive/root.xml">
      <name>application</name>
   </project>
   <project path="file:/C:/Dropbox/Public/xsl/recursive/project1.xml">
      <name>project1</name>
   </project>
   <project path="file:/C:/Dropbox/Public/xsl/recursive/project3.xml">
      <name>project3</name>
   </project>
   <project path="file:/C:/Dropbox/Public/xsl/recursive/project2.xml">
      <name>project2</name>
   </project>
</projects>