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>
Zire
  • 35
  • 5
  • It may not be finding the node because the 'xsl:' namespace is not defined. This link might help: http://stackoverflow.com/questions/443250/creating-a-specific-xml-document-using-namespaces-in-c-sharp – M3NTA7 Apr 09 '12 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. – Zire Apr 09 '12 at 18:58

2 Answers2

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

}

M3NTA7
  • 1,307
  • 1
  • 13
  • 25
  • 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.' – Zire Apr 09 '12 at 20:07
  • Are you still having issues? It would be helpful for us to see the code. That way we can see what you are trying. – M3NTA7 Apr 09 '12 at 20:29
  • Yes I have problem still may be I can send you xsl file ? – Zire Apr 09 '12 at 20:31
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

MiMo
  • 11,793
  • 1
  • 33
  • 48