XSLT 枚举每个子元素



我正在尝试创建一个超级通用的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、状态、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>

关闭...但实际上有点不同。只是想通了,让任何想知道的人知道:

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

使用相对路径,例如 <xsl:for-each select="*">而不是<xsl:for-each select="/*">.然后我认为你想放弃内部为每个人,简单地用

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

相关内容

  • 没有找到相关文章

最新更新