0

次の形式の xml ドキュメントがあり、xsl テンプレートを使用して変換したいと考えています。

私は xsl 変換の初心者であり、ツリーを再帰する方法を知る必要があるだけですが、問題全体の解決策があればいいのにと思います。

これは xml ドキュメントです。

<?xml version="1.0" encoding="UTF-8" ?>
<nodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <node>
        <type>Parent</type>
        <name>.test</name>
        <node>
            <type>parent</type>
            <name>.test.root</name>
            <node>
                <type>Parent</type>
                <name>.test.root.group</name>
                <node>
                    <type>int</type>
                    <name>.test.root.group.a</name>
                    <value>0</value>
                </node>
                <node>
                    <type>char</type>
                    <name>.test.root.group.b</name>
                    <value>-</value>
                </node>
            </node>
        </node>
        <node>
            <type>parent</type>
            <name>.test.versions</name>
            <node>
                <type>utf-8</type>
                <name>.test.versions.version</name>
                <value>alpha</value>
            </node>
            <node>
                <type>utf-8</type>
                <name>.test.version.extra</name>
                <value>16.5</value>
            </node>
        </node>
    </node>
</nodes>

そして、これは私が生成されたhtmlを次のようにしたい方法です:

    --------------------------------------------------。
    | | ツリー | 値 | タイプ |
    |------------------------+-----------+--------|
    | | '- テスト | | | 親|
    | | |- ルート | | | 親|
    | | | | '- グループ | | | 親|
    | | | | |-a | 0 | 整数 |
    | | | | '-b | - | チャー |
    | | '- バージョン | | | 親|
    | | |- バージョン | "アルファ" | utf-8 |
    | | '-余分な| 16.5 | utf-8 |
    '-------------------------------------------------'
4

1 に答える 1

3

この XSLT は、必要なツリーを生成します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="nodes/node">
      <xsl:with-param name="indent" select="''" />
      <xsl:with-param name="parent" select="''" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="node">
    <xsl:param name="indent"/>
    <xsl:param name="parent"/>

    <xsl:value-of select="$indent" />
    <xsl:value-of select="substring-after(name/text(), $parent)" />
    <xsl:text>&#xa;</xsl:text>

    <xsl:apply-templates select="./node">
      <xsl:with-param name="indent" select="concat($indent, '    |')" />
      <xsl:with-param name="parent" select="name/text()" />
    </xsl:apply-templates>

  </xsl:template>

</xsl:stylesheet>

次の 2 つの列にデータを追加するのは非常に簡単です。自分でやってみてください。

于 2010-06-04T08:44:01.667 に答える