简体   繁体   English

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

[英]Create List with XSLT from two xml docs

I have the following xml file: 我有以下xml文件:

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

I want to match each type up against the following list: 我想将每种类型与以下列表进行匹配:

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

Using this xslt with the "doc" being the xml and "types" the list above passed in as parameters: 将此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>

This should give me an unordered list for each sub in my xml printing the type from the sub and then each type. 这应该为我的xml中的每个子目录提供一个无序列表,从子目录中打印出类型,然后再打印每种类型。 When the sub and the type match, it should be bold. 当子类型匹配时,它应该为粗体。 I want this: 我要这个:

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

But I get this: 但是我得到这个:

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

Thanks for any and all help. 感谢您提供的所有帮助。

I think it might have something to do with the multiple uses of // . 我认为这可能与//的多种用法有关。

Try replacing your root template ( match="/" ) with this: 尝试用以下命令替换您的根模板( 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>

NOTE: I added the <html> tag to keep my output well-formed when testing. 注意:我添加了<html>标记,以在测试时保持输出格式正确。

Given your sub file as main input and the types in subtypes.xml 给定您的子文件作为主要输入,并指定subtypes.xml中的subtypes.xml

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


<xsl:template match="sub">
 <ul>
  <xsl:apply-templates select="doc('subtypes.xml')">
   <xsl:with-param name="this" select="." tunnel="yes"/>
  </xsl:apply-templates>
 </ul>
</xsl:template>

<xsl:template match="type">
 <xsl:param name="this" tunnel="yes"/>
 <li>
  <xsl:choose>
   <xsl:when test="$this/@type=.">
    <b><xsl:value-of select="$this/@type,' - ',."/></b>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$this/@type,' - ',."/>
   </xsl:otherwise>
  </xsl:choose>
 </li>
</xsl:template>

</xsl:stylesheet>

produces: 生产:

<?xml version="1.0" encoding="UTF-8"?>
  <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM