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

C xdocument:检查特定节点名是否存在,如果不存在,则添加

  •  -1
  • C Sharper  · 技术社区  · 6 年前

    我有以下节点,如果不存在,则需要在XSLT中添加这些节点:

    <xsl:template name="URLSpliter">
        <xsl:param name="url" />
        <xsl:variable name="splitURL" select="substring-after($url, '/')" />
        <xsl:if test="contains($splitURL, '/')">
          <!--To call the template recursively-->
          <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>
    

    为此,首先我需要检查它是否存在?-

    我查过了-

    IEnumerable<XElement> xElements = from xmlAuthor in doc.Descendants()
                                                          let xElement = xmlAuthor.Element("URLSpliter")
                                                          where xElement != null 
                                                          select xmlAuthor;
    
                        var IsUrlSplitterExists= xElements.Any();
    
                        if(IsUrlSplitterExists)
                        {
    
                        }
    

    1.我想知道是不是正确的方法?

    1. 如果不存在(element[name=“urlspliter”]),则需要添加。

    如何将其添加为XSLT的第一个节点?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Martin Honnen    6 年前

    要在带有linq to xml的XSLT命名空间中选择此类元素,您将使用

    XNamespace xsl = "http://www.w3.org/1999/XSL/Transform"; 
    if (!doc.Root.Elements(xsl + "template").Where(t => (string)t.Attribute("name") == "URLSplitter").Any()) { 
      doc.Root.AddFirst(new XElement(xsl + "template", new XAttribute("name", "URLSplitter"), ...)) 
    }
    

    当然,由于XSLT是XML,您也可以使用XSLT来操纵您的XSLT:

    <xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:axsl="http://www.w3.org/1999/XSL/Transform-alias"
      exclude-result-prefixes="axsl"
      version="1.0">
    
      <xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
    
      <xsl:output indent="yes"/>
    
      <xsl:param name="new-template">
       <axsl:template name="URLSpliter">
        <axsl:param name="url" />
        <axsl:variable name="splitURL" select="substring-after($url, '/')" />
        <axsl:if test="contains($splitURL, '/')">
          <!--To call the template recursively-->
          <axsl:call-template name="URLSpliter">
            <axsl:with-param name="url" select="$splitURL" />
          </axsl:call-template>
        </axsl:if>
        <axsl:if test="not(contains($splitURL, '/'))">
          <axsl:value-of select="$splitURL" />
        </axsl:if>
      </axsl:template>
     </xsl:param>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="xsl:transform[not(xsl:template[@name = 'URLSplitter'])] | xsl:stylesheet[not(xsl:template[@name = 'URLSplitter'])]">
        <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:copy-of select="$new-template"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    https://xsltfiddle.liberty-development.net/94rmq5T

    推荐文章