0

Android アクティビティ レイアウト xml ファイルを処理し、同等の HTML を作成する XSLT ファイルを作成しようとしています。

xslt ファイルの作成/テストでは、Eclipse xsltツールを使用しています。

このパスの障害の 1 つは、多くの値が Android レイアウト ファイルに直接保持されず (文字列/テキスト値など)、代わりに「values」フォルダーにある別の xml ファイルに保持されることです。

xslt 関数 'document' を使用して、strings.xml ファイルを開こうとしましたが、成功しませんでした。

  1. Q) xslt ドキュメント機能を動作させるために有効にする必要がある Eclipse 権限はありますか?

  2. Q) ドキュメント機能がどのように動作するべきかについて、私の理解に欠けているものはありますか?

アンドロイドstrings.xmlファイルにアクセスしようとしているTextViewテンプレート(xsltファイル内)に含まれる行は次のとおりです。

<xsl:value-of select="document('../values/strings.xml')/String[@name=substring-after(@android:text,'/')]" />

  • レイアウト ファイルは res/layout フォルダーにあります。
  • xslt は res/xsl フォルダーにあります。
  • strings.xml ファイルは res/values フォルダーにあります。

コードサンプルは次のとおりです。

Android XML レイアウト スニペット:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- This is the full screen vertical layout -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"    
    android:orientation="vertical" >
    <!-- This is the main content vertical layout -->


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:paddingLeft="@dimen/mainHeadingIndent" >
        <!--  This is the Status Section Vertical content layout -->

            <TextView
                style="@style/HeadingTextStyle"
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="@string/Status"/>
**The rest of the layout file is intentionally omitted**

XSLT ファイルは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:android="http://schemas.android.com/apk/res/android">
    <xsl:output method="html" version="4.0" encoding="iso-8859-1"
        indent="yes" />

    <xsl:template match="/">
        <html>
            <head>
            </head>
            <body>
                <xsl:for-each select="LinearLayout">
                    <xsl:call-template name="LinearLayout">
                    </xsl:call-template>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>

    <xsl:template name="LinearLayout">
        <xsl:variable name="width">
            <xsl:value-of select="@android:layout_width" />
        </xsl:variable>
        <xsl:variable name="height">
            <xsl:value-of select="@android:layout_height" />
        </xsl:variable>
        <xsl:variable name="margin">
            <xsl:value-of select="@android:layout_margin" />
        </xsl:variable>
        <xsl:variable name="orient">
            <xsl:value-of select="@android:orientation" />
        </xsl:variable>
        <xsl:variable name="pos">
            <xsl:number value="position()" format="1" />
        </xsl:variable>

        <div div_pos='{$pos}' Width='{$width}' Height='{$height}' Margin='{$margin}'
            Orient='{$orient}'>
            <xsl:for-each select="LinearLayout">
                <xsl:call-template name="LinearLayout">
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="TextView">
                <xsl:call-template name="TextView">
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="ImageButton">
                <xsl:call-template name="ImageButton">
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="Spinner">
                <xsl:call-template name="Spinner">
                </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="Button">
                <xsl:call-template name="Button">
                </xsl:call-template>
            </xsl:for-each>
        </div>
    </xsl:template>

    <xsl:template name="ImageButton">
        <div id="{@android:id}">
            <xsl:comment>
                ImageButton
            </xsl:comment>
        </div>
    </xsl:template>

    <xsl:template name="TextView">
        <xsl:variable name="pos">
            <xsl:number value="position()" format="1" />
        </xsl:variable>
        Text field =
        <xsl:value-of select="@android:text" />
        <xsl:variable name="txt">
            <xsl:choose>
                <xsl:when test="contains(@android:text,'/')">
                    <xsl:value-of select="document('../values/strings.xml')/String[@name=substring-after(@android:text,'/')]" />
                <!-- 
                    <xsl:value-of select="substring-after(@android:text,'/')" />
                -->
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="@android:text" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <div id="{@android:id}">
            <xsl:comment>
                TextView Element
            </xsl:comment>
            <p>
                Text pointer is: <xsl:value-of select="$txt" />
            </p>
            <!-- <p>Text pointer is:<xsl:copy-of select="$txt"/></p> <xsl:value-of 
                select="document('../values/strings.xml')/String[@name=$txt]" />
                    <xsl:value-of select="substring-after(@android:text,'/')" />
                -->
        </div>
        endoftext
        <br />
    </xsl:template>
    <xsl:template name="Spinner">
        <div id="{@android:id}">
            <xsl:comment>
                Spinner
            </xsl:comment>
        </div>
    </xsl:template>
    <xsl:template name="Button">
        <div id="{@android:id}">
            <xsl:comment>
                Button
            </xsl:comment>
        </div>
    </xsl:template>
</xsl:stylesheet>
4

2 に答える 2

0

さて、私はそれを解決しました。大きな問題は、xslt が失敗すると、通常は何も表示されずに失敗することです。つまり、エラーは発生しません。

問題はドキュメント機能自体ではないことが判明しました。ドキュメント機能に従ったのは選択基準でした。これには、選択されたノードがないため、ステートメントからの出力がないという問題がありました。これにより、ドキュメント機能が機能していないように見えました。

大きな変更点は、substring-after 句が適切な属性に対して評価されていない/評価されていないことです。部分文字列句を分割することで、機能し始めました。その名の通り「FM(フリッピングマジック)」です!

ドキュメント行を次のように置き換えました。

<xsl:variable name='string_value'>
  <xsl:value-of select="substring-after(@android:text,'/')" />
</xsl:variable>
<xsl:value-of select="$string_value"/>
<xsl:value-of select="document('../values/strings.xml')/*/string[@name=$string_value]"/>

これにより、strings.xml ファイルから正しい値が正常に取り出されました。

于 2013-05-24T23:18:14.980 に答える
0

ドキュメントのルート要素を XPath に含める必要があります (これは非常に忘れがちです)。

<xsl:value-of select="document('../values/strings.xml')/*/String[@name=substring-   after(@android:text,'/')]" />
于 2013-05-07T20:13:56.743 に答える