1

簡単なことはわかっていますが、試してみたところ、XSLT は私にとって本当に理解できませんでした...そのため、実際の例で調べて、おそらく理解できるように、コードの例を示したいと思います。

だから私はGUIツリービューを持ち、この種のXMLを生成するアプリを持っています:

<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="image" 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>

次に、この XML を HTML に変換して、自分の Web サイトで更新できるようにする必要があります。

<html>したがって、 and<body>タグは必要ありません。この XML をリストに順序付けするだけで済みますが、子要素の前にはスペースが必要です。

ブラウザでユーザーが表示する目的の出力は、次のようになります。

Installation
  Startup
  Getting there
  Steps
  Pictures
Usage
  Tilbud pane
  Report pane

しかし、属性には同じ値がないため、要素に基づいて順序付けする必要があります。

これは私が持っているものです:

<?xml version = "1.0" encoding = "utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <ul>
      <xsl:for-each select="Parent">
        <li>
        <xsl:value-of select="@text"/>
          <ul>
            <xsl:for-each select="Parent/child"/>
            <li>
            <xsl:value-of select="@text"/>
            </li>
            </xsl:for-each>
          </ul>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>

そして、私が取得しなければならない目的のhtmlは、次のようなものです:

<ul>
    <li>Parent
         <ul>
               <li>Child</li>
               <li>Child</li>
 .....
         </ul>
    </li>
   <li>Parent
         <ul>
               <li>Child</li>
               <li>Child</li>
 .....
         </ul>
   </li>
.....
</ul>

しかし、どうやらそれは私にこれを与えたくありません...<ul/>変換を実行した直後に私に与えます...

4

2 に答える 2

1

Parentあなたの XSLT は直接検索するため機能しません/TreeView、その間にあります。

<xsl:template match="/TreeView">
   ...
于 2013-08-19T14:30:38.540 に答える
0

変換を行うスタイルシートは次のとおりです。

<?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>
        <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:value-of select="@text"/></li>
    </xsl:template>

</xsl:stylesheet>

テキスト出力だけが必要な場合:

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

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

    <xsl:template match="Parent">
        <xsl:value-of select="@text"/>
        <xsl:text>&#xa;  </xsl:text>
        <xsl:apply-templates select="child"/>
    </xsl:template>

    <xsl:template match="child">
        <xsl:text>  </xsl:text>
        <xsl:value-of select="@text"/>
        <xsl:text>&#xa;  </xsl:text>
    </xsl:template>

</xsl:stylesheet>
于 2013-08-19T13:37:24.613 に答える