0

Could someone help me with this. I need in C# to remove node "xsl:variable" , as you can see here are two xsl:template nodes with child elements xsl:variable . In this sample I need to remove it with C#.

Something like this:

XmlDocument d = new XmlDocument();
d.Load("MyFileName.Xml");
XmlNode t = d.SelectSingleNode("/navigation/page[@id='1']");
t.ParentNode.RemoveChild(t);
d.Save();

But I cant get path of 'xsl:variable' to d.SelectSingleNode()

Please help me ?

THIS IS XSLT:

<xsl:template name="Aggregate:RealECBooleanToXMLBoolean">
        <xsl:param name="RealECBoolean" select="/.."/>
        <xsl:variable name="var1_result">
            <xsl:value-of select="($RealECBoolean = 'Yes')"/>
            <xsl:value-of select="($RealECBoolean = 'YES')"/>
            <xsl:value-of select="($RealECBoolean = 'X')"/>
        </xsl:variable>
        <xsl:variable name="var2_resultof_any" select="boolean(translate(normalize-space($var1_result), 'false0 ', ''))"/>
        <xsl:choose>
            <xsl:when test="string((string((string($var2_resultof_any) != 'false')) != 'false')) != 'false'">
                <xsl:value-of select="(string((string($var2_resultof_any) != 'false')) != 'false')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="false()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="Aggregate:LookupRECodeName">
        <xsl:param name="RECode" select="/.."/>
        <xsl:call-template name="vmf:vmf1_inputtoresult">
            <xsl:with-param name="input" select="$RECode"/>
        </xsl:call-template>
    </xsl:template>
2
  • It may not be finding the node because the 'xsl:' namespace is not defined. This link might help: stackoverflow.com/questions/443250/… Commented Apr 9, 2012 at 18:52
  • Thanks M3NTA7 I have a code for getting all xsl:template nodes but next step is to remove all child xsl:variable nodes and I cant do that with this sample upper. Need SingleNode value. Commented Apr 9, 2012 at 18:58

2 Answers 2

0

You need to passs to SelectNodes or SelectSingleNode a name space manager defining the xsl prefix:

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

and then:

XmlNode variableNode = xDoc.SelectSingleNode("//xsl:variable", namespaceManager);

will select the first xsl:variable element, or

XmlNodeList variableNodes = xDoc.SelectNodes("//xsl:variable", namespaceManager);

will select all xsl:variable elements

Sign up to request clarification or add additional context in comments.

Comments

0

Here's an idea. I'm not sure if you will need the 'xsl' name space when using SelectNodes, you will have to experiment with that.

public void YouMethod() {

XmlDocument doc = new XmlDocument();
doc.Load("your.xsl");

XmlNode root = doc.DocumentElement;

// iterate the template nodes
foreach (XmlNode tNode in root.SelectNodes("//xsl:template"))
{
  // iterate the variable child nodes
  foreach (XmlNode vNode in tNode.SelectNodes("xsl:variable"))
  {
      tNode.RemoveChild(vNode);
  } 
}

doc.Save();

}

3 Comments

Thanks again M3NTA7 I see that I have problem with root.SelectNodes("//xsl:template")) part. I was try to remove xsl but there is error again. I figure out that I need XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xDoc.NameTable); 'namespaceManager' and then got this error 'Namespace prefix 'xsl' is not defined.'
Are you still having issues? It would be helpful for us to see the code. That way we can see what you are trying.
Yes I have problem still may be I can send you xsl file ?

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.