XSLT用于动态树

时间:2016-03-26 14:09:51

标签: xslt

您可以在下面看到我的xml模式示例。每个 manuel 都有章节标记,章节标记可能包含 subchapter 核对清单标记。没有静态深度,它可能会将文档更改为文档,因此我无法编写动态递归xslt代码来生成Html。有谁知道,我怎么能做到这一点。

{
        "_id" : ObjectId("56f6966f007502d9952babde"),
        "name" : "World",
        "created" : ISODate("2016-03-26T14:02:23.546Z"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        50.201962,
                        1.128145
                ]
        }
}

对于这个样本,我想这个结果。我可以设置CSS样式,现在它不重要。问题在于产生结构。

  <manuel name="Test">
    <chapter name="00">

      <subchapter name="00.01">
        <checklist  name="00.01.01">
          <summary>abc</summary>
        </checklist>
      </subchapter>

      <subchapter name="00.02">
           <checklist  name="00.02.01">
               <summary>def</summary>
           </checklist>
           <subchapter name="00.02.02">
                <checklist  name="00.02.02.01">
                    <summary>xyz</summary>
                </checklist>
           </subchapter>
      </subchapter>

      <checklist  name="00.03">
            <summary>ZZZZ</summary>
      </checklist>
    </chapter>
  </manuel>

1 个答案:

答案 0 :(得分:0)

使用简单的apply-templates:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:template match="manuel">
    <xsl:apply-templates select=".//*"/>
</xsl:template>

<xsl:template match="chapter">
    <div class="cssChapter">
        <xsl:value-of select="@name"/>
    </div>
</xsl:template>

<xsl:template match="subchapter">
    <div class="cssSubChapter">
        <xsl:value-of select="@name"/>
    </div>
</xsl:template>

<xsl:template match="checklist">
    <div class="cssChecklist">
        <xsl:value-of select="@name"/>
    </div>
</xsl:template>

<xsl:template match="summary">
    <div class="cssSummary">
        <xsl:value-of select="."/>
    </div>
</xsl:template>

</xsl:transform>

http://xsltransform.net/3NJ3915/1在线。