使用 XSL 将 XML 列表转换为 XHTML 树

发布于 2024-09-04 00:00:13 字数 762 浏览 0 评论 0原文

我需要获取这个 xml…

<root>
   <item id=’1’ parent_id=’0’>ONE</item>
   <item id=’2’ parent_id=’1’>TWO</item>
   <item id=’3’ parent_id=’1’>THREE</item>
   <item id=’4’ parent_id=’2’>FOUR</item>
   <item id=’5’ parent_id=’0’>FIVE</item>
</root>

并生成这个 xhtml…

<div class=’parent’>ONE</div>
<div class=’child’>
   <div class=’parent’>TWO</div>
   <div class=’child’>
      <div class=’parent’>FOUR</div>
   </div>
   <div class=’parent’>THREE</div>
</div>
<div class=’parent’>FIVE</div>

我知道如何收集子节点并将它们放在父节点下,但是如果我已经将它们用作子节点,我无法掌握如何跳过将它们视为父节点。

I need to take this xml…

<root>
   <item id=’1’ parent_id=’0’>ONE</item>
   <item id=’2’ parent_id=’1’>TWO</item>
   <item id=’3’ parent_id=’1’>THREE</item>
   <item id=’4’ parent_id=’2’>FOUR</item>
   <item id=’5’ parent_id=’0’>FIVE</item>
</root>

And produce this xhtml…

<div class=’parent’>ONE</div>
<div class=’child’>
   <div class=’parent’>TWO</div>
   <div class=’child’>
      <div class=’parent’>FOUR</div>
   </div>
   <div class=’parent’>THREE</div>
</div>
<div class=’parent’>FIVE</div>

I get how to collect the child nodes and put them under their parent, but I can’t grasp how to then skip over considering them as parents if I have already used them as children.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-09-11 00:00:13

测试了以下内容,这似乎有效

应用 root_id = 0 的根目录中的所有项目,然后递归地迭代任何子项。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:apply-templates select="root/item[@parent_id='0']"/>
</xsl:template>


<xsl:template match="item">
  <div class="parent"><xsl:value-of select="."/></div>
  <xsl:if test="count(../item[@parent_id=current()/@id]) != 0">
    <div class="child">
      <xsl:apply-templates select="../item[@parent_id=current()/@id]"/>
    </div>
  </xsl:if>
</xsl:template>

Tested the following, which appears to work

Apply all items at the root with parent_id = 0, then recursively iterate for any children..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:apply-templates select="root/item[@parent_id='0']"/>
</xsl:template>


<xsl:template match="item">
  <div class="parent"><xsl:value-of select="."/></div>
  <xsl:if test="count(../item[@parent_id=current()/@id]) != 0">
    <div class="child">
      <xsl:apply-templates select="../item[@parent_id=current()/@id]"/>
    </div>
  </xsl:if>
</xsl:template>
物价感观 2024-09-11 00:00:13

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kItemById" match="item" use="@id"/>
    <xsl:key name="kItemByParentId" match="item" use="@parent_id"/>
    <xsl:template match="root">
        <xsl:apply-templates select="item[not(key('kItemById',@parent_id))]"/>
    </xsl:template>
    <xsl:template match="item">
        <xsl:variable name="vChild" select="key('kItemByParentId',@id)"/>
        <div class="parent">
            <xsl:value-of select="."/>
        </div>
        <xsl:if test="$vChild">
            <div class="child">
                <xsl:apply-templates select="$vChild"/>
            </div>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出:

<div class="parent">ONE</div>
<div class="child">
    <div class="parent">TWO</div>
    <div class="child">
        <div class="parent">FOUR</div>
    </div>
    <div class="parent">THREE</div>
</div>
<div class="parent">FIVE</div>

注意:交叉引用的键。

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kItemById" match="item" use="@id"/>
    <xsl:key name="kItemByParentId" match="item" use="@parent_id"/>
    <xsl:template match="root">
        <xsl:apply-templates select="item[not(key('kItemById',@parent_id))]"/>
    </xsl:template>
    <xsl:template match="item">
        <xsl:variable name="vChild" select="key('kItemByParentId',@id)"/>
        <div class="parent">
            <xsl:value-of select="."/>
        </div>
        <xsl:if test="$vChild">
            <div class="child">
                <xsl:apply-templates select="$vChild"/>
            </div>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output:

<div class="parent">ONE</div>
<div class="child">
    <div class="parent">TWO</div>
    <div class="child">
        <div class="parent">FOUR</div>
    </div>
    <div class="parent">THREE</div>
</div>
<div class="parent">FIVE</div>

Note: Keys for cross references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文