我想从C#代码中删除节点。
例如:我有下面的XSLT
<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> (其中的所有节点+该特定节点)
发布于 2019-02-12 13:55:58
您可以使用linq to xml并将其删除,如下所示
documentRoot
.Descendants("template")
.Where(ele=> (string) ele.Attribute("name") == "URLSpliter")
.Remove();工作示例:
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());发布于 2019-02-12 14:26:54
这段代码将为您工作。只需相应地替换路径即可。
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()));
}https://stackoverflow.com/questions/54643624
复制相似问题