如何使用xslt插入html单元格值?

时间:2014-11-21 12:38:15

标签: xslt-1.0

在下面的代码我有xml输入显示书籍类型。我想根据每列中的类型排列书籍类型。有些列的值不是。检查预期输出。

输入

<?xml version="1.0" encoding="UTF-8"?>
      <BookTypes>    
        <Types>
          <string>T1</string>
          <string>T3,M1,P1</string>
          <string>T2,P2</string>
          <string>M3,P3</string>      
        </Types>    
      </BookTypes>

XSLT脚本:当前的xslt脚本为我提供结果,但不是预期的结果。

    <xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="types">
    <type>T</type>
    <type>M</type>
    <type>P</type>
</xsl:variable>

<xsl:variable name="types-set" select="exsl:node-set($types)" />

<xsl:template match="/">
    <table>
       <tr>
            <th>T Type</th>
            <th>M Type</th>
            <th>P Type</th>
          </tr>
        <xsl:for-each select="BookTypes/Types/string">
       <tr>
          <xsl:variable name="splitValue">
            <xsl:apply-templates/>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($splitValue)/*">
            <xsl:variable name="mySplittedValue" select="." />
                  <xsl:for-each select="$types-set/type">
                   <xsl:variable name="my-types" select="." />
                    <td>
                       <xsl:choose>
                            <xsl:when test="contains($mySplittedValue, $my-types)">
                                 <xsl:value-of select="$mySplittedValue"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>-</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </td>
                </xsl:for-each>

          </xsl:for-each>
            </tr>
    </xsl:for-each>
     </table>            
</xsl:template>
<xsl:template match="text()" name="split">
    <xsl:param name="pText" select="."/>
    <xsl:if test="string-length($pText) > 0">
      <item>
        <xsl:value-of select="substring-before(concat($pText, ','), ',')"/>
      </item>
      <xsl:call-template name="split">
        <xsl:with-param name="pText" select="substring-after($pText, ',')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

预期输出

<table>
   <tr>
      <th>T Type</th>
      <th>M Type</th>
      <th>P Type</th>
   </tr>
   <tr>
      <td>T1</td>
      <td>-</td>
      <td>-</td>
   </tr>
   <tr>
      <td>T3</td>
      <td>M1</td>
      <td>P1</td>
   </tr>
   <tr>
      <td>T2</td>
      <td>-</td>
      <td>P2</td>
   </tr>
   <tr>
      <td>-</td>
      <td>M3</td>
      <td>P3</td>
   </tr>
</table>

总结:输出 有三(3)种固定类型,P,M,T,它们存在于任何一个输入中,例如P3,M3。这里P3包含P(类型),因此值P3应该在列名P类型下。输入中有3或2或1个值,逗号(比如T3,M1,P1)。应先将每个值拆分,然后再显示在表

1 个答案:

答案 0 :(得分:0)

我认为你要让它变得更加复杂。尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="types">
    <type>T</type>
    <type>M</type>
    <type>P</type>
</xsl:variable>

<xsl:variable name="types-set" select="exsl:node-set($types)" />

<xsl:template match="/">
    <table>>
        <tr>
            <xsl:for-each select="$types-set/type">
                <th><xsl:value-of select="concat(., ' Type')"/></th>
            </xsl:for-each>
        </tr>
        <xsl:for-each select="BookTypes/Types/string">
            <xsl:variable name="my-types" select="." />
            <tr>
                <xsl:for-each select="$types-set/type">
                    <td>
                        <xsl:choose>
                            <xsl:when test="contains($my-types, .)">
                                 <xsl:value-of select="."/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>-</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
     </table>            
</xsl:template>

</xsl:stylesheet>

<强>结果

<table>
   <tr>
      <th>T Type</th>
      <th>M Type</th>
      <th>P Type</th>
   </tr>
   <tr>
      <td>T</td>
      <td>-</td>
      <td>-</td>
   </tr>
   <tr>
      <td>T</td>
      <td>M</td>
      <td>P</td>
   </tr>
   <tr>
      <td>T</td>
      <td>-</td>
      <td>P</td>
   </tr>
   <tr>
      <td>-</td>
      <td>M</td>
      <td>P</td>
   </tr>
</table>