2

XML 構造:

<units>
    <unit>
        <lesson>
            <name>Sample 1</name>
            <sections>
                <section type="IN">
                    <title> Sample Title 1 </title>
                </section>
            </sections>
        </lesson>
        <lesson>
            <name>Sample 2</name>
            <sections>
                <section type="OF">
                    <title> Sample Title 2 </title>
                </section>
            </sections>
        </lesson>
        <lesson>
            <name>Sample 3</name>
            <sections>
                <section type="IN">
                    <title> Sample Title 3</title>
                </section>
            </sections>
        </lesson>
        <lesson>
            <name>Sample 4</name>
            <sections>
                <section type="AS">
                    <title> Sample Title 4</title>
                </section>
            </sections>
        </lesson>
        <lesson>
            <name>Sample 5</name>
            <sections>
                <section type="IN">
                    <title> Sample Title 5</title>
                </section>
            </sections>
        </lesson>
    </unit>
</units>

私の要件は、タイトル要素の値を取得して次のように表示することです(同様のデータと表示をグループ化)

IN:
Sample Title 1
Sample Title 3
Sample Title 5
OF:
Sample Title 2
AS:
Sample Title 5

期待される出力を得るために、次の兄弟オプションを使用しました。XML 構造が巨大であるため (スニペットのみを貼り付けました)、../../ およびすべて XSLT を使用してパスをハードコーディングすることはできません。期待される出力を得るのを手伝ってください。

4

2 に答える 2

3

兄弟軸のいずれかよりも、グループ化を使用してこれを解決することをお勧めします。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:key name="bySectionType" match="section" use="@type" />
    <xsl:template match="/">
        <xsl:apply-templates select="units/unit/lesson/sections/section" />
    </xsl:template>
    <xsl:template match="section" />
    <xsl:template
        match="section[generate-id()=
                       generate-id(key('bySectionType', @type)[1])]">
        <xsl:value-of select="concat(@type, ':&#xA;')" />
        <xsl:apply-templates select="key('bySectionType', @type)" mode="out" />
    </xsl:template>
    <xsl:template match="section" mode="out">
        <xsl:value-of select="concat(normalize-space(title), '&#xA;')" />
    </xsl:template>
</xsl:stylesheet>

出力:

IN:
Sample Title 1
Sample Title 3
Sample Title 5
OF:
Sample Title 2
AS:
Sample Title 4

preceding完全を期すために、次のスタイルシートはとfollowing軸を使用して同じ結果を達成します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:apply-templates select="units/unit/lesson/sections/section" />
    </xsl:template>
    <xsl:template match="section" />
    <xsl:template 
        match="section[not(preceding::section[@type=current()/@type])]">
        <xsl:value-of select="concat(@type, ':&#xA;')" />
        <xsl:apply-templates select=".|following::section[@type=current()/@type]"
            mode="out" />
    </xsl:template>
    <xsl:template match="section" mode="out">
        <xsl:value-of select="concat(normalize-space(title), '&#xA;')" />
    </xsl:template>
</xsl:stylesheet>

これははるかに効率の悪いソリューションです。これを解決する通常の方法は、上記のように、グループ化のためのミュンヒアン法を使用することです。

于 2011-04-26T18:40:08.823 に答える
1

XSLT 2.0 でのソリューションは次のとおりです。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />
    <xsl:template match="/">
        <xsl:for-each-group select="//section" group-by="@type">
           <xsl:value-of select="@type, ':&#xa;'" separator=""/>
           <xsl:value-of select="current-group()/title" separator="&#xA;" />
           <xsl:value-of select="'&#xa;'"/>
        </xsl:for-each-group>              
    </xsl:template>
</xsl:stylesheet>
于 2011-04-26T22:12:06.893 に答える