首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#从XSLT中删除整个节点

C#从XSLT中删除整个节点
EN

Stack Overflow用户
提问于 2019-02-12 13:53:01
回答 2查看 110关注 0票数 0

我想从C#代码中删除节点。

例如:我有下面的XSLT

代码语言:javascript
运行
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template name="URLSpliter">
    <xsl:param name="url" />
    <xsl:variable name="splitURL" select="substring - after($url, '/')" />
    <xsl:if test="contains($splitURL, '/')">
      <xsl:call-template name="URLSpliter">
        <xsl:with-param name="url" select="$splitURL" />
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="not(contains($splitURL, '/'))">
      <xsl:value-of select="$splitURL" />
    </xsl:if>
  </xsl:template>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />

在这里,我想删除整个urlsplitter拆分器节点和all中的所有节点

应该删除整个<xsl:template name="URLSpliter"> ...</template> (其中的所有节点+该特定节点)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-12 13:55:58

您可以使用linq to xml并将其删除,如下所示

代码语言:javascript
运行
复制
 documentRoot
           .Descendants("template")
           .Where(ele=> (string) ele.Attribute("name") == "URLSpliter")
           .Remove();

工作示例:

代码语言:javascript
运行
复制
XElement documentRoot  = 
              XElement.Parse (@"<ordersreport date='2012-08-01'>
                             <returns>
                              <template name='URLSpliter'>
                              </template>
                              <amount>

                                  <orderid>2</orderid>             
                                  <orderid>3</orderid>
                                  <orderid>21</orderid>
                                  <orderid>23</orderid>
                               </amount>
                             </returns>
                        </ordersreport>");
                documentRoot
               .Descendants("template")
               .Where(ele=> (string) ele.Attribute("name") == "URLSpliter")
               .Remove();


            Console.WriteLine(documentRoot.ToString());
票数 1
EN

Stack Overflow用户

发布于 2019-02-12 14:26:54

这段代码将为您工作。只需相应地替换路径即可。

代码语言:javascript
运行
复制
string xsltPath = @"C:\Users\ankushjain\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\XSLTFile.xslt";
string pathToSave = @"C:\Users\ankushjain\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\{0}.xslt";

XmlDocument xslDoc = new XmlDocument();
xslDoc.Load(xsltPath);

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xslDoc.NameTable);
namespaceManager.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

var nodesToDelete = xslDoc.SelectNodes("//xsl:template[@name='URLSpliter']", namespaceManager);

if (nodesToDelete != null & nodesToDelete.Count > 0)
{
    for (int i = nodesToDelete.Count - 1; i >= 0; i--)
    {
        nodesToDelete[i].ParentNode.RemoveChild(nodesToDelete[i]);
    }
    xslDoc.Save(string.Format(pathToSave, Guid.NewGuid()));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54643624

复制
相关文章

相似问题

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