【问题标题】:XSLT1: convert XML nodes to Html list, by starting from the deepest node firstXSLT1:将 XML 节点转换为 Html 列表,首先从最深的节点开始
【发布时间】:2011-05-12 03:46:52
【问题描述】:

我在xml下面有这个

 <root>
        <s>
            <name>self-1</name>
            <parents>
                <s>
                    <name>p-1-2</name>
                    <parents>
                        <s>
                            <name>p-1-2-1</name>
                            <parents>
                                <s>
                                    <name>p-1-2-1-1</name>
                                </s>
                            </parents>
                        </s>
                        <s>
                            <name>p-1-2-2</name>
                        </s>
                    </parents>
                </s>
            </parents>
        </s>
    </root>

我需要编写一个 xslt1 来解析该 xml 以产生如下所示的输出,目标是首先处理所有父节点,最后处理节点 self-1。请给我一些建议。

<ul>
    <li>p-1-2-1-1</li>
    <ul>
        <li>p-1-2-1</li>
        <li>p-1-2-2</li>
        <ul>
            <li>p-1-2</li>
            <ul>
                <!-- self -->
                <li>self-1</li>
            </ul>
        </ul>
    </ul>
</ul>

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    以下是我关于如何解决该问题的建议:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:output method="html" indent="yes"/>
    
      <xsl:template match="/">
        <xsl:apply-templates select="descendant::parents[not(s/parents)]"/>
      </xsl:template>
    
      <xsl:template match="parents | root">
        <ul>
          <xsl:apply-templates select="s/name"/>
          <xsl:variable name="p" select="parent::s/parent::parents | parent::s/parent::root"/>
          <xsl:if test="$p">
            <li>
              <xsl:apply-templates select="$p"/>
            </li>
          </xsl:if>
        </ul>
      </xsl:template>
    
      <xsl:template match="name">
        <li>
          <xsl:value-of select="."/>
        </li>
      </xsl:template>
    
    </xsl:stylesheet>
    

    输出

    <ul>
       <li>p-1-2-1-1</li>
       <li>
          <ul>
             <li>p-1-2-1</li>
             <li>p-1-2-2</li>
             <li>
                <ul>
                   <li>p-1-2</li>
                   <li>
                      <ul>
                         <li>self-1</li>
                      </ul>
                   </li>
                </ul>
             </li>
          </ul>
       </li>
    </ul>
    

    这不是您所要求的,但这是故意这样做的:您的样本有 ul 元素和 ul 子元素,但 HTML 中不允许这样做(http://www.w3.org/TR/ html4/struct/lists.html#h-10.2)。所以我的样式表通过确保任何ul 都只有li 子元素来确保结果是有效的HTML。

    【讨论】:

    • 非常感谢马丁,我完全忘记了我可以使用后代轴
    【解决方案2】:

    处理多个节点&lt;s&gt;

    我将模板&lt;xsl:template match="parents | root"&gt; 更新为如下内容:

     <xsl:template match="parents | root">
            <ul>
                <xsl:apply-templates select="s/name"/>
                <xsl:choose>
                    <xsl:when test="parent::s/parent::parents">
                        <xsl:variable name="p" select="parent::s/parent::parents"/>
                        <li><xsl:apply-templates select="$p"/></li>
                    </xsl:when>
    
                    <xsl:when test="parent::s/parent::root">
                        <xsl:variable name="p" select="parent::s/parent::root"/>
                        <li><xsl:apply-templates select="$p"/></li>
                    </xsl:when>
                </xsl:choose>
            </ul>
        </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 2014-07-16
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 2019-12-23
      • 2014-11-21
      相关资源
      最近更新 更多