XSLT枚举每个子元素

时间:2013-09-12 17:40:23

标签: css xml xslt styles

我正在尝试创建一个超级通用的XSLT,它基本上只能依靠表和&行元素在那里。我有以下示例XML我正在尝试为其创建一个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<table>
    <row>
        <id>5311</id>
        <status>Active</status>
        <id_vendor>Verizon</id_vendor>
        <mobile_number>555123456</mobile_number>
    </row>
    <row>
        <id>5312</id>
        <status>Inactive</status>
        <id_vendor>Sprint</id_vendor>
        <mobile_number>555123457</mobile_number>
    </row>
    <row>
        <id>5313</id>
        <status>Active</status>
        <id_vendor>ATT</id_vendor>
        <mobile_number>555123458</mobile_number>
    </row>
</table>

问题在于我不知道这些字段最终会是什么(id,status,id_vendor,mobile_number等都可能会发生变化。

我想看看如下的观点是很愉快的:

<div style="background-color:#E3CA87;padding:.2em">
    <div style="background-color:#F1E2BB;padding:.2em">
        <div>id: x<div>
        <div>status: active<div>
        <div>id_vendor: Verizon<div>
        <div>mobile_number: 555123456<div>
    <div>
<div>

这是我到目前为止所拥有的...但我无法让匿名元素正确:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="background-color:#eee;font-size:12pt;font-family:Arial;">
    <xsl:for-each select="table/row">
      <div style="background-color:#eee;padding:.5em">

       <xsl:for-each select="/*">

       <div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
        <xsl:for-each select="./*">
          <div style="background-color:#F1E2BB;padding:.2em">
<xsl:value-of select ="name(./*)"/>: 
<xsl:value-of select="*" />
          </div>
        </xsl:for-each>
      </div>
    </xsl:for-each>
      </div>
    </xsl:for-each>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

关闭......但它实际上有点不同......只是想知道任何想知道的人:

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="background-color:#eee;font-size:12pt;font-family:Arial;">
    <xsl:for-each select="table/row">
      <div style="background-color:#eee;padding:.5em">

       <xsl:for-each select=".">

       <div style="background-color:#E3CA87;padding:.2em;border: 1px solid black">
        <xsl:for-each select=".">
<xsl:for-each select="./*">
          <div style="background-color:#F1E2BB;padding:.2em">
 <xsl:value-of select="concat(local-name(.), ': ', .)" />
          </div>        
</xsl:for-each>
        </xsl:for-each>
      </div>
    </xsl:for-each>
      </div>
    </xsl:for-each>
  </body>
</html>

答案 1 :(得分:0)

使用相对路径,例如<xsl:for-each select="*">代替<xsl:for-each select="/*">。然后我想你想要删除内部for-each并简单地用

替换它
<div style="background-color:#F1E2BB;padding:.2em">
           <xsl:value-of select="concat(local-name(), ': ', .)" />
          </div>