0

From C# code I want to delete node from XSLT.

Eg. I have below 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" />

Here I want to delete entire urlsplitter node and all the nodes within URLSplitter

Entire <xsl:template name="URLSpliter"> ...</template> should get deleted (All the nodes within + that particular node )

4
  • You mean all the nodes having name equals to "URLSpliter"? Commented Feb 12, 2019 at 5:55
  • yes.. all nodes having name="urlsplitter" .. if it is within any node , then also I want to remove that parent node Commented Feb 12, 2019 at 5:57
  • There are two different types of nodes that have URLSplitter name. First, <xsl:template name="URLSpliter"> & second - <xsl:call-template name="URLSpliter">. You want to delete both? Commented Feb 12, 2019 at 6:02
  • Entire nodes within <xsl:template name="URLSpliter"> and </template> @AnkushJain Commented Feb 12, 2019 at 6:02

2 Answers 2

1

you can use linq to xml and remove it like as below

 documentRoot
           .Descendants("template")
           .Where(ele=> (string) ele.Attribute("name") == "URLSpliter")
           .Remove();

Working sample :

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());
Sign up to request clarification or add additional context in comments.

4 Comments

@CSharper - can you try now
It is not working since it has value in name attribute .. Eg. <xsl:template name="URLSpliter">
@CSharper - updated code have look and make adjustment as per your xml
Removing "template" from .Descendants("template") worked
1

This piece of code will work for you. Just replace the path accordingly.

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()));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.