簡體   English   中英

XSLT:每個孩子都有一部分的div

[英]XSLT: for-each div with part of childs

我從很短的時間開始使用XSL,因此必須使用部分child標記創建多個div。 所以我有這樣的事情:

<Nodes>
    <Node>
        <Tag>a</Tag>
        <Tag>b</Tag>
    </Node>
    <Node>
        <Tag>c</Tag>
    </Node>
</Nodes>

我以為我可以做這樣的事情:

<xsl:for-each select="/Nodes">
    <div id="node_{position()}">
        <xsl:for-each select="Node">
            <xsl:value-of select="Tag" />
        </xsl:for-each>
    </div>
</xsl:for-each>

我需要的是:

<div>
    a
    b
</div>
<div>
    c
</div>

但是我總是和AB C一起得到兩個div。 相反,第一個使用ab,另一個使用c。 我是否必須枚舉標簽或類似的東西?

編輯:

<ProjectTopology>
    <Nodes>
        <Node>
            <Tag>Section1</Tag>
            <Nodes>
                <Node>
                    <Tag>Another section1</Tag>
                    <Tag>Another section2</Tag>
                </Node>
            </Nodes>
            <Tag>Section2</Tag>
            <Nodes>
                <Node>
                    <Tag>Another section3</Tag>
                    <Tag>Another section4</Tag>
                </Node>
            </Nodes>
        </Node>
    </Nodes>
</ProjectTopology>

好的,我現在正在尋找這樣的東西:

<div id="section_1">
    Another section1
    Another section2
</div>
<div id="section_2">
    Another section3
    Another section4
</div>

但是我總是和AB C一起得到兩個div。

不,這不是應用您在此處發布的代碼的結果。 實際結果是:

<div id="node_1">ac</div>

在XSLT 1.0中,以及:

<div id="node_1">a bc</div>

在XSLT 2.0中。

輸出中只有一個div ,因為源XML中只有一個Nodes節點-並且唯一一個創建div模板是一個匹配的Nodes

為了獲得想要的結果,您應該嘗試類似的方法:

XSLT 1.0

<xsl:template match="/Nodes">
    <root>
        <xsl:for-each select="Node">
            <div id="node_{position()}">
                <xsl:for-each select="Tag">
                    <xsl:value-of select="." />
                </xsl:for-each>
            </div>
        </xsl:for-each>
    </root>
</xsl:template>

結果

<root>
   <div id="node_1">ab</div>
   <div id="node_2">c</div>
</root>

XSLT 2.0

<xsl:template match="/Nodes">
    <root>
        <xsl:for-each select="Node">
            <div id="node_{position()}">
                <xsl:value-of select="Tag" />
            </div>
        </xsl:for-each>
    </root>
</xsl:template>

結果

<root>
   <div id="node_1">a b</div>
   <div id="node_2">c</div>
</root>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM