C#从XSLT中删除整个节点
从C代码中,我想从XSLT中删除节点 我有以下XSLTC#从XSLT中删除整个节点,c#,asp.net,.net,xml,xslt,C#,Asp.net,.net,Xml,Xslt,从C代码中,我想从XSLT中删除节点 我有以下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
<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" />
在这里,我想删除整个urlspilter节点和urlspilter中的所有节点
整个
…
应该被删除(该特定节点内的所有节点)您可以使用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());
XElement documentRoot=
XElement.Parse(@)
2.
3.
21
23
");
文档根
.子体(“模板”)
.Where(ele=>(string)ele.Attribute(“name”)==“urlspilter”)
.Remove();
Console.WriteLine(documentRoot.ToString());
您可以使用linq转换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());
XElement documentRoot=
XElement.Parse(@)
2.
3.
21
23
");
文档根
.子体(“模板”)
.Where(ele=>(string)ele.Attribute(“name”)==“urlspilter”)
.Remove();
Console.WriteLine(documentRoot.ToString());
这段代码适合您。只需相应地替换路径即可
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()));
}
这段代码将适用于您。只需相应地替换路径即可
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()));
}
你是说所有的节点的名称都等于“urlspilter”?是的。。具有name=“urlspilter”的所有节点。。如果它位于任何节点内,那么我还想删除该父节点。有两种不同类型的节点具有URLSPILTER名称。第一,第二-。是否要同时删除?和@Ankushjain中的整个节点?您是指名称等于“Urlspilter”的所有节点?是。。具有name=“urlspilter”的所有节点。。如果它位于任何节点内,那么我还想删除该父节点。有两种不同类型的节点具有URLSPILTER名称。第一,第二-。是否要删除和中的整个节点@AnkushJain@CSharper-您现在可以试试吗?它不工作,因为它在名称属性中有值。。例如@CSharper-更新的代码具有外观,并根据您的XML进行调整。从.substands(“模板”)中删除“模板”worked@CSharper-您现在可以试试吗?它不工作,因为它在名称属性中有值。。例如@CSharper-更新的代码具有外观,并根据XML进行调整。从子体(“模板”)中删除“模板”起作用