如何在eclipse中启用/使用XSLT文档功能



我正在尝试编写一个XSLT文件,该文件将处理Android Activity布局xml文件并创建一个等效的HTML。

在创建/测试xslt文件时,我使用了eclipse xslt工具。 在这条路径上的障碍之一,是许多值不直接保存在Android布局文件(如字符串/文本值),而是保存在一个单独的xml文件位于'values'文件夹

我一直在尝试使用xslt函数'document'打开strings.xml文件,但没有成功。

  1. Q)我是否需要启用eclipse权限才能允许xslt文档函数操作?

  2. Q)我对文档功能应该如何操作的理解中是否缺少了什么?

包含在TextView模板(在xslt文件中)中试图访问android strings.xml文件的行是:

<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 Layout代码段:

 <?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>

您需要在XPath中包含文档的根元素(这很容易忘记):

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

好了,我已经解决了。最大的问题是,当xslt失败时,它通常无声地失败....没有错误

问题原来不是文档功能本身。这是遵循文档功能的选择标准。这就产生了一个问题,即没有选择节点,因此语句没有输出。这一切都使文档功能看起来没有工作。

最大的变化是substring-after子句没有/没有针对正确的属性进行计算。通过拆分substring子句,它开始工作。正如他们所说,这是"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文件中取出了正确的值。

最新更新