xml - 从两个xml文档使用XSLT创建列表

标签 xml xslt xpath

我有以下xml文件:

<root>
  <sub type="print">print</sub>
  <sub type="email">email</sub>
</root>


我想将每种类型与以下列表进行匹配:

<types>
  <type>email</type>
  <type>broadcast</type>
  <type>mobile</type>
  <type>print</type>
  <type>web</type>
</types>


将此xslt与“ doc”作为xml并使用“ types”一起使用时,上面的列表作为参数传入:

<xsl:stylesheet 
  xmlns = "http://www.w3.org/1999/xhtml"
  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
  version="2.0">

  <xsl:output method="xhtml" indent="yes" />

  <xsl:param name="doc"/>
  <xsl:param name="types"/>   

  <xsl:template match="/">
    <xsl:for-each select="$doc//sub">
      <xsl:variable name="count">
        <xsl:number value="position()" format="1"/>
      </xsl:variable>

      <ul>
        <xsl:for-each select="$types//type">
          <xsl:choose>
            <xsl:when test="$doc//sub[$count]/@type = text()">
              <li>
                <b>
                  <xsl:value-of select="$doc//sub[$count]/@type"/> - <xsl:value-of select="text()"/>
                </b>
              </li>
            </xsl:when>
            <xsl:otherwise>
              <li>
                <xsl:value-of select="$doc//sub[$count]/@type"/> - <xsl:value-of select="text()"/>
              </li>
            </xsl:otherwise>
          </xsl:choose> 
        </xsl:for-each>
      </ul>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>


这应该为我的xml中的每个子目录提供一个无序列表,从子目录中打印出类型,然后再打印每种类型。当子类型匹配时,它应为粗体。我要这个:

<ul>
  <li>print - email</li>
  <li>print - broadcast</li>
  <li>print - mobile</li>
  <li><b>print - print</b></li>
  <li>print - web</li>
</ul>
<ul>
  <li><b>email - email</b></li>
  <li>email - broadcast</li>
  <li>email - mobile</li>
  <li>email - print</li>
  <li>email - web</li>
</ul>


但是我明白了:

<ul>
  <li><b>print email - email</b></li>
  <li>print email - broadcast</li>
  <li>print email - mobile</li>
  <li><b>print email - print</b></li>
  <li>print email - web</li>
</ul>
<ul>
  <li><b>print email - email</b></li>
  <li>print email - broadcast</li>
  <li>print email - mobile</li>
  <li><b>print email - print</b></li>
  <li>print email - web</li>
</ul>


感谢您提供的所有帮助。

最佳答案

我认为这可能与//的多种用途有关。

尝试用以下命令替换您的根模板(match="/"):

  <xsl:template match="/">
    <html>
      <xsl:for-each select="$doc/root/sub">
        <xsl:variable name="vType" select="@type"/>
        <ul>
          <xsl:for-each select="$types/types/type">
            <li>
              <xsl:choose>
                <xsl:when test=".=$vType">
                  <b>
                    <xsl:value-of select="concat($vType,' - ',.)"/>
                  </b>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="concat($vType,' - ',.)"/>
                </xsl:otherwise>
              </xsl:choose>            
            </li>
          </xsl:for-each>
        </ul>
      </xsl:for-each>      
    </html>
  </xsl:template>


注意:我添加了<html>标记以在测试时保持输出格式正确。

关于xml - 从两个xml文档使用XSLT创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669616/

相关文章:

android - ViewFlipper 宽度正在包裹其内容并且 fill_parent 不起作用

php - 无法在 php 中读取雅虎 xml 提要

xml - 使用 <xsl :apply-templates> and <xsl:sort>

xslt - Symphony CMS中奇怪的XSL输出

xml - 如何使用xsl 2.0函数

xpath - lxml xpath 函数返回的元素是否按顺序返回?

xml - 获取 XSLT 中带有前缀的 XML 标记的值

XSL 文件中的 Java setParameter

xml - XPath 可以跨 XML 的两个子树执行外键查找吗?

xslt - 如何限制 XSLT 1.0 中的字符串字数?