首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPATH表达式match="TAGNAME“匹配,但match=”父/TAGNAME“不匹配

XPATH表达式match="TAGNAME“匹配,但match=”父/TAGNAME“不匹配
EN

Stack Overflow用户
提问于 2016-11-15 11:14:00
回答 1查看 65关注 0票数 0

我是XSLT新手,希望从csproj中的引用中删除所有“私有”标记,然后在每个"HintPath“后面引入HintPath”False“标记。我为此写了一个工作解决方案,却不完全理解我在做什么:

XSLT

代码语言:javascript
复制
    <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>

示例输入文件:

代码语言:javascript
复制
<?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的任务?

我不喜欢这样的事实,如果“私有”标签出现在文档中而不是“引用”中,可能会有附带损害,但我没有找到一种方法来指定它,包括那个父标记。有什么暗示吗?

第二个问题:是否有更简单的方法来处理命名空间?

欢迎任何建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-15 11:24:42

使用前缀限定所有元素的名称:<xsl:template match="DUMMY:Reference/DUMMY:Private"/>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40608428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档