[英]Create List with XSLT from two xml docs
我有以下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>
標記,以在測試時保持輸出格式正確。
給定您的子文件作為主要輸入,並指定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>
生產:
<?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.