Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 如何使用apply templates调用模板并将一个属性作为参数传递_Xslt - Fatal编程技术网

Xslt 如何使用apply templates调用模板并将一个属性作为参数传递

Xslt 如何使用apply templates调用模板并将一个属性作为参数传递,xslt,Xslt,我有一个类似于下面的XML <siteMap> <siteMapNode id="1232" title="Home" url="www.google.com" depth="0" use_as_default="Yes"> <siteMapNode id="" title="Resourses" url="" depth="1" blue_button="False"> <siteMapNode id="

我有一个类似于下面的XML

<siteMap>
    <siteMapNode id="1232" title="Home" url="www.google.com" depth="0" use_as_default="Yes">
        <siteMapNode id="" title="Resourses" url="" depth="1" blue_button="False">
            <siteMapNode id="" title="Project" url="" depth="2" blue_button="False" />
            <siteMapNode id="" title="Music" url="" depth="2" blue_button="False" />
            <siteMapNode id="" title="Vedio" url="" depth="2" blue_button="False" />
            <siteMapNode id="" title="Party" url="" depth="2" blue_button="False" /></siteMapNode>
    </siteMapNode>
</siteMap>

在XSLT中的某个地方,我想编写类似这样的代码

 <xsl:template match="/">
    <ul class="toplevel-menu group">
      <xsl:apply-templates select="EXPERSSION1" />
    </ul>
  </xsl:template>

  <xsl:template match="EXPRESSION2">
    <li>
    <a>
      <xsl:attribute name="title">
        <xsl:value-of select="@title"/>
      </xsl:attribute>
      <xsl:attribute name="href">
        <xsl:value-of select="@url"/>
      </xsl:attribute>
      <xsl:value-of select="@title"/>
    </a>
    </li>
  </xsl:template>

  • 如何编写select=“expersion1”和match=“EXPRESSION2”

    如果要将“深度”属性传递到 有人能帮我在场景中构建选配体验吗

    更新

    有没有可能写这样的东西-

    <xsl:apply-templates select="child::*[@depth='2']">
                     <xsl:with-param name="depth" select="2" />
        </xsl:apply-templates>
    
    
    
    并使用它如下

      <xsl:template match="sm:siteMapNode[@depth='2']"> 
             <xsl:if test="child::*">
             <xsl:apply-templates select="child::*">
             <xsl:with-param name="depth" select="$depth+1" />   
             </xsl:apply-templates>
             </xsl:if> 
              </xsl:template>
    
    
    
    因为我必须更新一个现有的xslt,并且希望更改最少,以避免破坏任何东西。xslt示例:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
        <xsl:template match="/">
            <ul class="toplevel-menu group">
                <xsl:apply-templates select="siteMap/siteMapNode">
                    <xsl:with-param name="depth" select="1" />
                </xsl:apply-templates>
            </ul>
        </xsl:template>
        <xsl:template match="siteMapNode">
            <xsl:param name="depth" />
            <li>
                <a>
                    <xsl:attribute name="title">
                        <xsl:value-of select="@title" />
                    </xsl:attribute>
                    <!-- you could output the depth as attribute if you like, or use it some other way -->
                    <xsl:attribute name="depth">
                        <xsl:value-of select="$depth" />
                    </xsl:attribute>
                    <xsl:attribute name="href">
                        <xsl:value-of select="@url" />
                    </xsl:attribute>
                    <xsl:value-of select="@title" />
                </a>
                <xsl:if test="siteMapNode">
                    <xsl:apply-templates select="siteMapNode">
                        <xsl:with-param name="depth" select="$depth+1" />
                    </xsl:apply-templates>
                </xsl:if>
            </li>
        </xsl:template>
    </xsl:stylesheet>
    
    
    
  • 更新后编辑:

    <xsl:template match="siteMapNode">
        <xsl:param name="depth" />
        <xsl:if test="$depth=2">
            <xsl:apply-templates select="siteMapNode" mode="depth2"/>
        </xsl:if>
        <xsl:if test="not($depth=2)">
            <!-- original template -->
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="siteMapNode" mode="depth2">
        <li depth="2"/>
    </xsl:template>
    
    
    
  • 您不需要将深度作为参数传递,因为每个
    siteMapNode
    在原始XML中已经有一个
    depth
    属性,因此您可以直接使用这些属性

    <xsl:template match="sm:siteMap">
      <ul class="toplevel-menu group">
        <xsl:apply-templates select="sm:siteMapNode" />
      </ul>
    </xsl:template>
    
    <xsl:template match="sm:siteMapNode">
      <li>
        <a title="{@title}" href="{@url}">
          <xsl:value-of select="@title"/>
        </a>
        <!-- example of doing something with the depth attribute -->
        <xsl:text> at depth </xsl:text>
        <xsl:value-of select="@depth"/>
    
        <xsl:if test="sm:siteMapNode">
          <ul><xsl:apply-templates select="sm:siteMapNode" /></ul>
        </xsl:if>
      </li>
    </xsl:template>
    
    
    
  • 在深处

    • 在这种情况下,您希望输出是什么样子?您是要构建一个与原始节点嵌套匹配的嵌套列表,还是一次创建一个所有siteMapNodes的平面列表?我想显示嵌套的
      • 意思,如果有任何子节点,则使用
      • 放置新的
          • X
          • Y
            • a
              • 并向前嵌套。非常感谢您的回复。+1的快速回复。你能看看我的更新上面,并建议我相应吗