我是XSLT新手,希望从csproj中的引用中删除所有“私有”标记,然后在每个"HintPath“后面引入HintPath”False“标记。我为此写了一个工作解决方案,却不完全理解我在做什么:
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:DUMMY="http://schemas.microsoft.com/developer/msbuild/2003"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- use DUMMY namespace as default, would otherwise write missing ns explicitly -->
<xsl:output encoding="UTF-8" indent="yes"/> <!-- encoding as required for output file, indent=yes: would write all text in one line otherwise -->
<xsl:strip-space elements="*"/> <!-- delete all white spaces -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Private"/> <!-- obsolete line: without the namespace workaround: no match -->
<xsl:template match="DUMMY:Private"/> <!-- working line: only deletes tags itself, not indention -->
<xsl:template match="DUMMY:Reference/Private"/> <!-- obsolete line: Does not match! -->
<xsl:template match="DUMMY:Reference//Private"/> <!-- obsolete line: Does also not match! -->
<xsl:template match="DUMMY:HintPath">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
<xsl:element name="Private">
<xsl:text>False</xsl:text>
</xsl:element>
</xsl:template>
</xsl:stylesheet>示例输入文件:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!-- The original document has an anonymous default namespace -->
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<ItemGroup>
<Reference Include="Configuration">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)..\..\public\Bin\$(Configuration)\Configuration.dll</HintPath>
</Reference>
<Reference Include="Core">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)..\..\public\Bin\$(Configuration)\Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Data">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)..\..\public\Bin\$(Configuration)\Data.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
</Project>如前所述:这对我来说是可行的,但到目前为止,找出这个问题还是相当复杂的。是否有更好的方法来完成XSLT的任务?
我不喜欢这样的事实,如果“私有”标签出现在文档中而不是“引用”中,可能会有附带损害,但我没有找到一种方法来指定它,包括那个父标记。有什么暗示吗?
第二个问题:是否有更简单的方法来处理命名空间?
欢迎任何建议。
发布于 2016-11-15 11:24:42
使用前缀限定所有元素的名称:<xsl:template match="DUMMY:Reference/DUMMY:Private"/>。
https://stackoverflow.com/questions/40608428
复制相似问题