1

「スイッチ」のような条件ステートメントを作成しようとしているのでtype、XML ファイルの属性の値に応じて、HTML に特定のアイコンを配置してから、テキストを追加します。しかし、完全に機能していない場合...または私が望むとおりではありません。

XML:

<?xml version="1.0" encoding="utf-8"?>
<TreeView>
  <Parent text="Installation">
    <child text="Startup" type="video" file="startup.wmv" icon="c://user/desktop/blahblah.jpg"/>
    <child text="Getting there" type="video" file="gettingthere.wmv" icontPath="something"/>
    <child text="Steps" type="document" file="steps.docx" icon="asd"/>
    <child text="Pictures" type="presentation" file="pics.jpg" icon="dasdasdas"/>
  </Parent>
  <Parent text="Usage">
    <child text="Tilbud pane" type="video" file="tilbud.mwv" icon="asdasd"/>
    <child text="Report pane" type="document" file="report.docx" icon="gfdhd"/>
  </Parent>
</TreeView>

XSLT:

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

    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="/">
        <ul id="LinkedList1" class="LinkedList">
        <xsl:apply-templates/>
        </ul>
    </xsl:template>

    <xsl:template match="Parent">
        <li>
        <xsl:value-of select="@text"/>
        <br/>
        <ul>
          <xsl:choose>
            <xsl:when test="child/@type = 'video'">
              <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" />
            </xsl:when>
            <xsl:when test="child/@type = 'document'">
              <img src="word_icon.png" alt="text_document_icon" title="Text Document" />
            </xsl:when>
            <xsl:when test="child/@type = 'presentation'">
              <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point presentation" />
            </xsl:when>
          </xsl:choose>
          <xsl:apply-templates select="child"/>
        </ul>
        </li>
    </xsl:template>

    <xsl:template match="child">
        <li><xsl:value-of select="@text"/></li>
    </xsl:template>

</xsl:stylesheet>

これは私が得るものです - >

![Not correct.][1]
------------------------------------

そして、これが私が望む方法です->

![Correct.][2]
------------------------------------

条件文を正しく並べていないと思いますが、どうすればいいのかわかりません。それで、これを修正する方法はありますか?

4

1 に答える 1

2

これを試してください

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

    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="/">
        <ul id="LinkedList1" class="LinkedList">
        <xsl:apply-templates/>
        </ul>
    </xsl:template>

    <xsl:template match="Parent">
        <li>
            <xsl:value-of select="@text"/>
            <br/>
            <ul>
              <xsl:apply-templates select="child"/>
            </ul>
        </li>
    </xsl:template>

    <xsl:template match="child[@type='video']">
        <li>
            <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" />
            <xsl:value-of select="@text"/>
        </li>
    </xsl:template>
    <xsl:template match="child[@type='document']">
        <li>
              <img src="word_icon.png" alt="text_document_icon" title="Text Document" />
            <xsl:value-of select="@text"/>
        </li>
    </xsl:template>
    <xsl:template match="child[@type='presentation']">
        <li>
            <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point  presentation" />
            <xsl:value-of select="@text"/>
        </li>
    </xsl:template>
</xsl:stylesheet>

あなたのアプローチの問題は、親ごとに 1 つの <img> を生成するだけであることです。

必要に応じて、これも使用できます

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

    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="/">
        <ul id="LinkedList1" class="LinkedList">
        <xsl:apply-templates/>
        </ul>
    </xsl:template>

    <xsl:template match="Parent">
        <li>
            <xsl:value-of select="@text"/>
            <br/>
            <ul>
              <xsl:apply-templates select="child"/>
            </ul>
        </li>
    </xsl:template>

    <xsl:template match="child">
        <li>
            <xsl:choose>
                <xsl:when test="@type = 'video'">
                  <img src="play_icon.png" alt="play_video_icon" title="Video tutorial" />
                </xsl:when>
                <xsl:when test="@type = 'document'">
                  <img src="word_icon.png" alt="text_document_icon" title="Text Document" />
                </xsl:when>
                <xsl:when test="@type = 'presentation'">
                  <img src="powerpoint_icon.png" alt="powerpoint_icon" title="Power Point presentation" />
                </xsl:when>
            </xsl:choose>
            <xsl:value-of select="@text"/>
        </li>
    </xsl:template>
</xsl:stylesheet>
于 2013-08-21T09:42:40.230 に答える