我是XSLT的新手,你能帮我解决我的xslt代码问题。我试图遍历每个xml记录,并根据每个Record xml条目的多个bu和主元数据生成li链接。我试图用HTML输出如下,文档和市场bu /主要行是超链接。
必需输出:
Title 1 - Document BU and Primary sample links
Document primary title one
Document bu title one
Document bu title two
Document bu title three
Document bu title four
Title 2 - Market BU and Primary sample links
Market primary title one
Market bu title one
Market bu title two
Market bu title three
Market bu title four
XSLT代码:
<xsl:template match="R">
<tr>
<td>
<xsl:choose>
<xsl:when test="starts-with(U, 'http://abc.domain.com/') ">
<xsl:variable name="count">
<xsl:value-of select="count(meta[@name='primary' or @name='bu'])"/>
</xsl:variable>
<ul class="policy_list">
<xsl:call-template name="more_manuals">
<xsl:with-param name="baseurl" select="U" />
<xsl:with-param name="count" select="$count" />
</xsl:call-template>
</ul>
<p>
<xsl:value-of select="$snip" disable-output-escaping='yes' />
</p>
</xsl:when>
<xsl:otherwise>
<!-- Some other logic -->
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
<xsl:template name="other_manuals">
<xsl:param name="baseurl" />
<xsl:param name="count" />
<xsl:call-template name="iterate_meta_data">
<xsl:with-param name="counter" select="1" />
<xsl:with-param name="meta" select="meta[@name='primary' or @name='bu'][1]" />
<xsl:with-param name="baseurl" select="$baseurl" />
<xsl:with-param name="count" select="$count" />
</xsl:call-template>
</xsl:template>
<xsl:template name="iterate_meta_data">
<xsl:param name="counter" />
<xsl:param name="meta" />
<xsl:param name="baseurl" />
<xsl:param name="count" />
<xsl:choose>
<xsl:when test="$meta">
<xsl:variable name="meta_name_1"><xsl:value-of select="$meta/@value"/>_name</xsl:variable>
<xsl:variable name="meta_name_2"><xsl:value-of select="$meta/@value"/></xsl:variable>
<xsl:variable name="cs" select="$meta/following-sibling::meta[@name=$meta_name_2]/@value" />
<xsl:variable name="manualName" select="$meta/following-sibling::meta[@name=$meta_name_1]/@value" />
<xsl:variable name="ch" select="substring-before($cs, '|')" />
<xsl:variable name="dns" select="substring-after($cs, '|')" />
<xsl:variable name="sitename">
<xsl:call-template name="get_site_name">
<xsl:with-param name="dns" select="$dns"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="manurl">
<xsl:variable name="bu">
<xsl:if test="$meta/@name = 'bu'">
<xsl:value-of select="concat('&bu=', $meta/@value)" disable-output-escaping='yes'/>
</xsl:if>
</xsl:variable>
<xsl:value-of select="concat($baseurl, '&site=', $dns, '&ch=', $ch, $bu)" disable-output-escaping='yes'/>
</xsl:variable>
<li><a href="{$manurl}" onClick="backButtonSave(this)"><xsl:value-of select="$sitename"/>: <xsl:value-of select="$manualName"/></a></li>
<xsl:call-template name="iterate_meta_data">
<xsl:with-param name="counter" select="$counter + 1" />
<xsl:with-param name="meta" select="$meta/following-sibling::meta[@name='primary' or @name='bu']" />
<xsl:with-param name="baseurl" select="$baseurl"/>
<xsl:with-param name="count" select="$count"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
示例XML条目:
-
<xml>
<R N="1" L="1" MIME="text/html">
<U>http://abc.domain.com/sample</U>
<T>Title 1 - Document BU and Primary sample links</T>
<MT N="CT" V="Market" />
<MT N="SITES" V="abc" />
<MT N="AUTHOR" V="amarnath, koduru" />
<MT N="primary" V="asdwer12332334234123" />
<MT N="asdwer12332334234123" V="123456789|abc" />
<MT N="asdwer12332334234123_name" V="Document primary title one" />
<MT N="bu" V="werou2309weir23940293" />
<MT N="werou2309weir23940293" V="0909898werweiuouw|cde" />
<MT N="werou2309weir23940293_name" V="Document bu title one" />
<MT N="bu" V="xvxddc2309wei345weeffw" />
<MT N="xvxddc2309wei345weeffw" V="ue983479573hww5|cde" />
<MT N="xvxddc2309wei345weeffw_name" V="Document bu title two" />
<MT N="bu" V="jljwerwoi798872334oweor" />
<MT N="jljwerwoi798872334oweor" V="hrei9873495739uwe|cde" />
<MT N="jljwerwoi798872334oweor_name" V="Document bu title three" />
<MT N="bu" V="nnm23334hweiuroweir2" />
<MT N="nnm23334hweiuroweir2" V="iuw3242ouwejlkeo|cde" />
<MT N="nnm23334hweiuroweir2_name" V="Document bu title four" />
<MT N="TID" V="NT" />
<S><b>...</b>Document Provide details. Share your research <b>...</b></S>
<LANG>en</LANG>
</R>
<R N="2" L="1" MIME="text/html">
<U>http://abc.domain.com/sample</U>
<T>Title 2 - Market BU and Primary sample links</T>
<MT N="CT" V="Market" />
<MT N="SITES" V="abc" />
<MT N="AUTHOR" V="amarnath, koduru" />
<MT N="primary" V="wwasdwer12332334234123" />
<MT N="wwasdwer12332334234123" V="123456789|abc" />
<MT N="wwasdwer12332334234123_name" V="Market primary title one" />
<MT N="bu" V="ddwerou2309weir23940293" />
<MT N="ddwerou2309weir23940293" V="0909898werweiuouw|cde" />
<MT N="ddwerou2309weir23940293_name" V="Market bu title one" />
<MT N="bu" V="ggxvxddc2309wei345weeffw" />
<MT N="ggxvxddc2309wei345weeffw" V="ue983479573hww5|cde" />
<MT N="ggxvxddc2309wei345weeffw_name" V="Market bu title two" />
<MT N="bu" V="bbjljwerwoi798872334oweor" />
<MT N="bbjljwerwoi798872334oweor" V="hrei9873495739uwe|cde" />
<MT N="bbjljwerwoi798872334oweor_name" V="Market bu title three" />
<MT N="bu" V="ssnnm23334hweiuroweir2" />
<MT N="ssnnm23334hweiuroweir2" V="iuw3242ouwejlkeo|cde" />
<MT N="ssnnm23334hweiuroweir2_name" V="Market bu title four" />
<MT N="TID" V="NT" />
<S><b>...</b> Market Provide details. Share your research <b>...</b></S>
<LANG>en</LANG>
</R>
</xml>
答案 0 :(得分:2)
使用xslt时需要注意的一点是变量是不可变的。 XSLT是一种声明性的函数式编程语言,对于任何函数式语言,变量都是不可变的。
允许传入参数并且可以正常工作,但如果依靠该值来改变迭代,则在第一个模板中设置计数变量将不起作用。
下面的代码示例提供下面的输出,可以使用CSS格式化以满足您的需要。
Title 1 - Document BU and Primary sample links
primary : asdwer12332334234123
bu : werou2309weir23940293
bu : xvxddc2309wei345weeffw
bu : jljwerwoi798872334oweor
bu : nnm23334hweiuroweir2
S : ...Document Provide details. Share your research ...
LANG : en
Title 2 - Market BU and Primary sample links
primary : wwasdwer12332334234123
bu : ddwerou2309weir23940293
bu : ggxvxddc2309wei345weeffw
bu : bbjljwerwoi798872334oweor
bu : ssnnm23334hweiuroweir2
S : ... Market Provide details. Share your research ...
LANG : en
使用XSL将XML更改为样式
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<xml>
<R N="1" L="1" MIME="text/html">
<U>http://abc.domain.com/sample</U>
<T>Title 1 - Document BU and Primary sample links</T>
<MT N="CT" V="Market" />
<MT N="SITES" V="abc" />
<MT N="AUTHOR" V="amarnath, koduru" />
<MT N="primary" V="asdwer12332334234123" />
<MT N="asdwer12332334234123" V="123456789|abc" />
<MT N="asdwer12332334234123_name" V="Document primary title one" />
<MT N="bu" V="werou2309weir23940293" />
<MT N="werou2309weir23940293" V="0909898werweiuouw|cde" />
<MT N="werou2309weir23940293_name" V="Document bu title one" />
<MT N="bu" V="xvxddc2309wei345weeffw" />
<MT N="xvxddc2309wei345weeffw" V="ue983479573hww5|cde" />
<MT N="xvxddc2309wei345weeffw_name" V="Document bu title two" />
<MT N="bu" V="jljwerwoi798872334oweor" />
<MT N="jljwerwoi798872334oweor" V="hrei9873495739uwe|cde" />
<MT N="jljwerwoi798872334oweor_name" V="Document bu title three" />
<MT N="bu" V="nnm23334hweiuroweir2" />
<MT N="nnm23334hweiuroweir2" V="iuw3242ouwejlkeo|cde" />
<MT N="nnm23334hweiuroweir2_name" V="Document bu title four" />
<MT N="TID" V="NT" />
<S><b>...</b>Document Provide details. Share your research <b>...</b> </S>
<LANG>en</LANG>
</R>
<R N="2" L="1" MIME="text/html">
<U>http://abc.domain.com/sample</U>
<T>Title 2 - Market BU and Primary sample links</T>
<MT N="CT" V="Market" />
<MT N="SITES" V="abc" />
<MT N="AUTHOR" V="amarnath, koduru" />
<MT N="primary" V="wwasdwer12332334234123" />
<MT N="wwasdwer12332334234123" V="123456789|abc" />
<MT N="wwasdwer12332334234123_name" V="Market primary title one" />
<MT N="bu" V="ddwerou2309weir23940293" />
<MT N="ddwerou2309weir23940293" V="0909898werweiuouw|cde" />
<MT N="ddwerou2309weir23940293_name" V="Market bu title one" />
<MT N="bu" V="ggxvxddc2309wei345weeffw" />
<MT N="ggxvxddc2309wei345weeffw" V="ue983479573hww5|cde" />
<MT N="ggxvxddc2309wei345weeffw_name" V="Market bu title two" />
<MT N="bu" V="bbjljwerwoi798872334oweor" />
<MT N="bbjljwerwoi798872334oweor" V="hrei9873495739uwe|cde" />
<MT N="bbjljwerwoi798872334oweor_name" V="Market bu title three" />
<MT N="bu" V="ssnnm23334hweiuroweir2" />
<MT N="ssnnm23334hweiuroweir2" V="iuw3242ouwejlkeo|cde" />
<MT N="ssnnm23334hweiuroweir2_name" V="Market bu title four" />
<MT N="TID" V="NT" />
<S><b>...</b> Market Provide details. Share your research <b>...</b></S>
<LANG>en</LANG>
</R>
</xml>
XSLT用于使用HTML / CSS创建上面的输出,您可以轻松获取数据并以纯文本格式输出或格式化为另一个XML文档。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style>
ul li {
list-style-type: none;
}
</style>
</head>
<body>
<xsl:for-each select="xml/R">
<xsl:value-of select="T" />
<ul>
<xsl:for-each select="MT">
<xsl:if test="boolean(@N = 'primary' or @N ='bu')">
<li>
<xsl:value-of select="@N" /> : <xsl:value-of select="@V" />
</li>
</xsl:if>
</xsl:for-each>
<li>S : <xsl:value-of select="S" /></li>
<li>LANG : <xsl:value-of select="LANG" /></li>
</ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>