没有输出为html视图

时间:2015-11-04 08:00:08

标签: xml xslt

我有一个XML文件,它读取关联XSLT文件,然后我可以在网络浏览器中看到输出为HTML。应该有一个表,其中第一列是组件的名称' XML文件中的节点,第二列是组件的描述 但经过一些改变后,我无法获得任何所需的输出。它只显示一个空表,如下图所示。

enter image description here

你能帮帮我吗?

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xslt"?>
<Summary>
    <test name="test">
        <xml_name name="ABC">
            <version num="104">
                <component name="APPS">
                    <componenet_ver>104</componenet_ver>
                </component>
            <component name="Ner">
                <componenet_ver>1.0</componenet_ver>
                </component>
                <component name="HUNE">
                    <componenet_ver>003</componenet_ver>
                </component>
                <component name="FADA">
                    <componenet_ver>107</componenet_ver>
                </component>
                <component name="VEDA">
                    <componenet_ver>8.8</componenet_ver>
                </component>
            </version>
        </xml_name>
    </test>
</Summary>

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" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Summary/test">
    <html>
        <body>
            <table>
                <tr bgcolor="Peru">
                    <th>Components</th>
                    <th>Versions</th>
                </tr>
                <xsl:for-each select="//component">
                    <xsl:variable name="CompomName" select="@name"/>
                    <xsl:variable name="VerName" select="description"/>
                    <tr>
                       <td bgcolor="aqua" name = "{$CompomName}"> </td>
                       <td bgcolor="aqua" name = "{$VerName}"> </td>
                    </tr>
                </xsl:for-each>                                 
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

3 个答案:

答案 0 :(得分:2)

您的样式表格式不正确:您缺少第二个 public class MainActivity extends Activity { SharedPreferences sharedpreferences; TextView name; TextView email; public static final String mypreference = "mypref"; public static final String Name = "nameKey"; public static final String Email = "emailKey"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { name.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { email.setText(sharedpreferences.getString(Email, "")); } } public void Save(View view) { String n = name.getText().toString(); String e = email.getText().toString(); Editor editor = sharedpreferences.edit(); editor.putString(Name, n); editor.putString(Email, e); editor.commit(); } public void clear(View view) { name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); name.setText(""); email.setText(""); } public void Get(View view) { name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { name.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { email.setText(sharedpreferences.getString(Email, "")); } } 以及<tr><xsl:for-each>说明的结束标记。

加了:

  

它只显示一个空表。

该表为空,因为表格单元格没有内容(空格除外)。试试这种方式吗?

</xsl:stylesheet>

不确定你是否真的需要命名细胞,我把它留在了。

请注意,您的<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="5.0" encoding="UTF-8" indent="yes"/> <xsl:template match="Summary/test"> <html> <body> <table> <tr bgcolor="Peru"> <th>Components</th> <th>Versions</th> </tr> <xsl:for-each select="//component"> <xsl:variable name="CompomName" select="@name"/> <xsl:variable name="VerName" select="componenet_ver"/> <tr> <td bgcolor="aqua" name="{$CompomName}"> <xsl:value-of select="$CompomName"/> </td> <td bgcolor="aqua" name="{$VerName}"> <xsl:value-of select="$VerName"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> 未选择任何内容。

答案 1 :(得分:1)

尝试此操作(更正缺失的结束标记):

<?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" version="5.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Summary/test">
    <html>
        <body>
            <table>
                <tr bgcolor="Peru">
                    <th>Components</th>
                    <th>Versions</th>
                    </tr>
                        <xsl:for-each select="//component">
                        <xsl:variable name="CompomName" select="@name"/>
                        <xsl:variable name="VerName" select="./componenet_ver"/>
                    <tr>
                    <td bgcolor="aqua" name = "{$CompomName}"><xsl:value-of select="$CompomName"/></td>
                    <td bgcolor="aqua" name = "{$VerName}"><xsl:value-of select="$VerName"/></td>  
</tr>                                 
</xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

答案 2 :(得分:0)

几个更正:

1 /您需要将值写为缺少元素的文本。

修正:

<td bgcolor="aqua" name = "{$CompomName}">
    <xsl:value-of select="$CompomName" />
</td>
<td bgcolor="aqua" name = "{$VerName}">
    <xsl:value-of select="$VerName" /> 
</td>

2 /对于变量VerName,您正在选择不存在的“description”元素。你可能想要的是选择“component_ver”元素如下:

<xsl:variable name="VerName" select="componenet_ver"/>

尝试这些2,它会起作用。

enter image description here