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

Xslt 1.0 如何使用xslt插入html单元格值?,xslt-1.0,Xslt 1.0,在下面的代码中,我有显示图书类型的xml输入。我想根据每列中的类型排列图书类型。有些列有值,有些列没有。检查预期输出 输入 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"

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

输入

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>
总结:产出
有三种固定类型,P、M、T存在于任何一个输入中,如P3、M3。此处P3包含P类型,因此值P3应位于列名P类型下。在输入中,有3、2或1个值被逗号分隔,比如T3、M1、P1。每个值都应该先被分割,然后再显示在表中,我认为这使它变得更加复杂。尝试:

XSLT1.0

结果:


答案是正确的,但不适用于我的场景。如果我考虑第二输入格式,它将无法工作。M,P i只是为了缓解你,而不是实际输入。假设你可以考虑输入M,P等是IP地址。@ ADP,我们只能回答你问的问题,你问他们的方式。如果您的场景不同,请编辑您的问题并解释如何进行。我期待第二次输入的第二次预期输出。请再次检查详细信息。提供的输入值随附M、P、T。您有打印的类型,但我想打印包含sayM1、P1、45P、2M9类型的值。M、P和T类型是固定的。包含任何一种类型的值,该类型应位于类型值之下。假设M1值应包含在M类型列下。在一行中,最多有三个值由couma分隔,。我想按这些类型打印每个值。这里是打印类型,我想打印由逗号分隔的每一行的值,第二个预期输出您使用的是哪个特定XSLT 1.0处理器?关于您编辑的第二个输出。一个例子不足以解释转换背后的逻辑。请提供准确的规则-例如:字符串的第一个字符,或?有三种固定类型,P、M、T存在于任何一个输入中,如P3、M3。此处P3包含P类型,因此值P3应位于列名P类型下。在输入中,有3、2或1个值被逗号分隔,比如T3、M1、P1。每个值都应该先拆分,然后再根据它们的类型显示在表中。@michael我已经更新了您的脚本并在我的问题中上载了。执行它并检查它的输出,这可能很容易理解,以便您可以指导我。
<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>
<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>