Solved!
在遵循 Matti 的建议后,我删除了自定义函数,一切都很好。
原帖:
截至今天,我还是 XSLT 的新手,所以我确信这对你们中的许多人来说是理所当然的。无论如何:
我的任务是使用第三方供应商提供的数据为我公司的网站创建一个小部件。
供应商拒绝向我们发送示例 XML 文件(甚至是只有元素标签的空白标签!)所以我尝试根据我在他们发送给我们的 XSLT 中看到的内容重新创建 XML。 (可笑之处比比皆是)
这是我们收到的(已删除的)XSLT 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myCustXslFunctions="urn:CustomXslFunctions">
<xsl:variable name="NumberColumns" >1</xsl:variable>
<xsl:variable name="PaperId" >1234567890ABCDEF</xsl:variable>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="no" />
<xsl:template match="/NewDataSet">
<div><xsl:apply-templates select="/NewDataSet" mode="columns" /></div>
</xsl:template>
<xsl:template match="NewDataSet" mode="columns">
<xsl:for-each select="Table[position() mod $NumberColumns = 1 or $NumberColumns = 1]">
<p>
<xsl:for-each select=".|following-sibling::Table[position() < $NumberColumns]">
<span class="description">
<xsl:element name="a">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:attribute name="class" >description</xsl:attribute>
<xsl:choose>
<xsl:when test="retail='true'">
<xsl:attribute name="href">http://website/retail/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="imageurl != ''">
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="imageurl" /></xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
<xsl:attribute name="class">thumbnail</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="img">
<xsl:attribute name="src">http://website/thumbs/<xsl:value-of select="paperid" />_<xsl:value-of select="paperitemid" />_100.jpg</xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
<xsl:attribute name="class">thumbnail</xsl:attribute>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</span>
</xsl:for-each>
</p>
<p>
<xsl:for-each select=".|following-sibling::Table[position() < $NumberColumns]">
<span class="description">
<xsl:element name="a">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:attribute name="class" >description</xsl:attribute>
<xsl:choose>
<xsl:when test="retail='true'">
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="string-length(shortdescr) = 0"><xsl:value-of select="myCustXslFunctions:MakeNice(descr,20,20,'Left','true')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="myCustXslFunctions:MakeNice(shortdescr,20,20,'Left','true')" /></xsl:otherwise>
</xsl:choose>
</xsl:element>
</span>
</xsl:for-each>
</p>
</xsl:for-each>
</xsl:template>
</xsl:transform>
我对 XML 进行逆向工程的无力尝试:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="facepalm.xsl"?>
<NewDataSet>
<Table>
<paperid>123</paperid>
<paperitemid>12345</paperitemid>
<descr>facepalm of doom</descr>
<shortdescr>facepalm</shortdescr>
<retail>true</retail>
<imageurl>http://website/facepalm.jpg</imageurl>
</Table>
<Table>
<paperid>456</paperid>
<paperitemid>67890</paperitemid>
<descr>mega-sigh</descr>
<shortdescr>sigh</shortdescr>
<retail>true</retail>
<imageurl>http://website/sigh.jpg</imageurl>
</Table>
</NewDataSet>
毫无疑问,我忽略了一些简单的事情,但我的 XSLT 新手身份已经使这成为一个需要数小时的项目。
任何帮助是极大的赞赏。