2017-04-24 67 views
1

新手到xslt。我有一个无法更改的xml文件,如下所示。 id属性将存在于所有元素中,但其他变量可能存在也可能不存在。任何有助于在xslt中表达这一点的方法都非常感谢!谢谢!xsl帮助html表

<A> 
    <B> 
     <att id="1" var1="ABC" var2="DEF" /> 
     <att id="2" var2="FGD" /> 
     <att id="3" var1="ABC" var3="KQR" /> 
    </B> 
    <C> 
     <att id="5" var1="ABC" var2="DEF" /> 
     <att id="8" var3="FGD" /> 
     <att id="9" var1="ABC" var4="KQR" /> 
    </C> 
    <D> 
     <att id="11" var1="ABC" var2="DEF" /> 
     <att id="13" var5="FGD" var6="TRE" /> 
     <att id="14" var1="ABC" var3="KQR" /> 
    </D> 
</A> 

我想创建HTML表格,看起来像下面:

A (Header) 
B (Sub-Header) 
-------------------------- 
|ID | Var1 | Var2 | Var3 | 
|1 | ABC | DEF |  | 
|2 |  | FGD |  | 
|3 | ABC |  | KQR | 
-------------------------- 
C (Sub-Header) 
--------------------------------- 
|ID | Var1 | Var2 | Var3 | Var4 | 
|5 | ABC | DEF |  | 
|8 |  |  | FGD |  |  
|9 | ABC |  |  | KQR | 
--------------------------------- 
D (Sub-Header) 
---------------------------------------- 
|ID | Var1 | Var2 | Var3 | Var5 | Var6 | 
|11 | ABC | DEF |  |  |  | 
|13 |  |  |  | FGD | TRE | 
|14 | ABC |  | KQR |  |  | 
---------------------------------------- 
+0

看看这可以让你开始:http://stackoverflow.com/a/21744053/3016153 –

回答

1

哇,棘手的一个...在XSLT 1.0分组(作为问题被标记)的范围内,并在属性名称。

像这样的事情似乎工作...

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 

<xsl:key name="kAttsByGrandparent" match="@*[not(local-name() = 'id')]" use="generate-id(ancestor::*[2])"/> 
<xsl:key name="kDistinctAttsByGrandparent" match="@*[not(local-name() = 'id')]" use="concat(generate-id(ancestor::*[2]), '||', local-name())"/> 

<xsl:key name="distinct" match="att" use="."/> 
<xsl:template match="/"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="*"> 
    <h1> 
     <xsl:value-of select="local-name()"/> 
    </h1> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="*[att]"> 
    <h1> 
     <xsl:value-of select="local-name()"/> 
    </h1> 
    <xsl:variable name="vGrandparentId" select="generate-id()"/> 
    <xsl:variable name="vDistinctAttNames" select="key('kAttsByGrandparent', $vGrandparentId)[generate-id() = generate-id(key('kDistinctAttsByGrandparent', concat($vGrandparentId, '||', local-name()))[1])]"/> 
    <table border="1"> 
     <!-- header row --> 
     <tr> 
      <th> 
       <xsl:text>ID</xsl:text> 
      </th> 
      <xsl:for-each select="$vDistinctAttNames"> 
       <xsl:sort select="local-name()"/> 
       <th> 
        <xsl:value-of select="local-name()"/> 
       </th> 
      </xsl:for-each> 
     </tr> 
     <xsl:apply-templates select="att"> 
      <xsl:with-param name="pDistinctAttNames" select="$vDistinctAttNames"/> 
     </xsl:apply-templates> 
    </table> 
    <xsl:apply-templates select="*[not(local-name() = 'att')]"/> 
</xsl:template> 

<xsl:template match="att"> 
    <xsl:param name="pDistinctAttNames"/> 
    <tr> 
     <td> 
      <xsl:value-of select="@id"/> 
     </td> 
     <xsl:variable name="vThisElement" select="."/> 
     <xsl:for-each select="$pDistinctAttNames"> 
      <xsl:sort select="local-name()"/> 
      <td> 
       <xsl:variable name="vAttName" select="local-name()"/> 
       <xsl:value-of select="$vThisElement/@*[local-name() = $vAttName]"/> 
      </td> 
     </xsl:for-each> 
    </tr> 
</xsl:template> 
</xsl:stylesheet> 
+0

是的,这是做到了。谢谢!在我的“真正的XSL”我不得不删除 的 ,因为它是错误的头文件映射到值。再次感谢! – Snath

+0

@Snath - 在编辑它之前,您可能已经抓住了代码......我忘记了第二个。 –

0

我就与https://www.w3schools.com/xml/tryxslt.asp在线和xsltproc上CLI去。

XSLT的文件

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<body> 
    <h1>A</h1> 
    <h2>B</h2> 
    <table border="1"> 
<tr><th>id</th><th>var1</th><th>var2</th><th>var3</th></tr> 
    <xsl:for-each select="/A/B/att"> 
    <tr> 
     <td><xsl:value-of select="@id"/></td> 
     <td><xsl:value-of select="@var1"/></td> 
     <td><xsl:value-of select="@var2"/></td> 
     <td><xsl:value-of select="@var3"/></td> 
    </tr> 
    </xsl:for-each> 
    </table> 
    <h2>C</h2> 
    <table border="1"> 
<tr><th>id</th><th>var1</th><th>var2</th><th>var3</th><th>var4</th></tr> 
    <xsl:for-each select="/A/C/att"> 
    <tr> 
     <td><xsl:value-of select="@id"/></td> 
     <td><xsl:value-of select="@var1"/></td> 
     <td><xsl:value-of select="@var2"/></td> 
     <td><xsl:value-of select="@var3"/></td> 
     <td><xsl:value-of select="@var4"/></td> 
    </tr> 
    </xsl:for-each> 
    </table> 
    <h2>D</h2> 
    <table border="1"> 
<tr><th>id</th><th>var1</th><th>var2</th><th>var3</th><th>var5</th><th>var6</th></tr> 
    <xsl:for-each select="/A/D/att"> 
    <tr> 
     <td><xsl:value-of select="@id"/></td> 
     <td><xsl:value-of select="@var1"/></td> 
     <td><xsl:value-of select="@var2"/></td> 
     <td><xsl:value-of select="@var3"/></td> 
     <td><xsl:value-of select="@var5"/></td> 
     <td><xsl:value-of select="@var6"/></td> 
    </tr> 
    </xsl:for-each> 
    </table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

可以例如叫它xsltproc template.xslt data.xml

+0

感谢您的答复。实际上我想动态获取标题和值,因为有很多元素,并且可以在以后将新元素添加到xml中。我很抱歉没有提前指出这一点。再次感谢! – Snath

+0

也许下一次;) – vv01f