代码之家  ›  专栏  ›  技术社区  ›  C Sharper

从XSLT中删除整个节点

  •  0
  • C Sharper  · 技术社区  · 6 年前

    从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 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节点和urlsplitter中的所有节点

    整个 <xsl:template name="URLSpliter"> ...</template> 应该被删除(在+特定节点内的所有节点)

    2 回复  |  直到 6 年前
        1
  •  1
  •   Pranay Rana    6 年前

    您可以使用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());
    
        2
  •  1
  •   Ankush Jain    6 年前

    这段代码对你有用。只需相应地替换路径。

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