我从这个表中获取了查找表,如果字段(buyeritemcode=substring@id=‘0’,11,3),则从查找表中取subfamily=subfamily,否则取'9':
<lookup>
<Code>
<BuyerItemCode>439</BuyerItemCode>
<Subfamily>016</Subfamily>
</Code>
</lookup> Xml文件如下所示:
<document>
<line id="14">
<field id="0"><![CDATA[MMM4443 419280600000]]></field>
</line>
<line id="15">
<field id="0"><![CDATA[MMM4443 414390600000]]></field>
</line>
</document>我需要用lookup.xml比较这个数据,如果数据不能比较插入常量9。用altova v11我的程序能用,用cooktop就不行,我的意思是比较是假的。我的程序看起来:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="date exsl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="ProdSubfamily" match="Subfamily" use="../BuyerItemCode"/>
<xsl:template match="/">
<Interchange>
<Group>
<Message>
<xsl:if test="/document/line[(substring(field[@id='0'], 1,3)='MMM')]">
<xsl:apply-templates mode="MMM" select="/document"/>
</xsl:if>
</Message>
</Group>
</Interchange>
</xsl:template>
<xsl:template mode="MMM" match="/document">
<PriceCatalogue-Lines>
<xsl:for-each select="/document/line[contains(substring(field[@id='0'], 1,3),'MMM') and not(contains(substring(field[@id='0'],9,1),'0'))]">
<xsl:variable name="inputProd" select="substring(field[@id='0'], 11,3)"/>
<Line>
<Line-Item>
<LineNumber>
<xsl:value-of select="position()"/>
</LineNumber>
<BuyerItemCode>
<xsl:value-of select="substring(field[@id='0'], 11,3)"/>
</BuyerItemCode>
<SubFamily>
<xsl:choose>
<xsl:when test="substring(field[@id='0'], 11,3) = document('lookup.xml')/*/*/BuyerItemCode">
<xsl:for-each select="document('lookup.xml')">
<xsl:for-each select="key('ProdSubfamily',$inputProd)">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'9'"/>
</xsl:otherwise>
</xsl:choose>
</SubFamily>
</Line-Item>
</Line>
</xsl:for-each>
</PriceCatalogue-Lines>
</xsl:template>
</xsl:stylesheet>我用Altova得到的正确结果,我想用cooktop得到这个结果:
<Interchange>
<Group>
<Message>
<PriceCatalogue-Lines>
<Line>
<Line-Item>
<LineNumber>1</LineNumber>
<BuyerItemCode>928</BuyerItemCode>
<SubFamily>9</SubFamily>
</Line-Item>
</Line>
<Line>
<Line-Item>
<LineNumber>2</LineNumber>
<BuyerItemCode>439</BuyerItemCode>
<SubFamily>016</SubFamily>
</Line-Item>
</Line>
</PriceCatalogue-Lines>
</Message>
</Group>
</Interchange>我用Cooktop得到的坏结果是:
<Interchange>
<Group>
<Message>
<PriceCatalogue-Lines>
<Line>
<Line-Item>
<LineNumber>1</LineNumber>
<BuyerItemCode>928</BuyerItemCode>
<SubFamily>9</SubFamily>
</Line-Item>
</Line>
<Line>
<Line-Item>
<LineNumber>2</LineNumber>
<BuyerItemCode>439</BuyerItemCode>
<SubFamily>9</SubFamily>
</Line-Item>
</Line>
</PriceCatalogue-Lines>
</Message>
</Group>
</Interchange>发布于 2011-05-12 10:59:38
问题出在源XML文档中。
CDATA部分包含不必要的[字符,它是文本节点的第一个字符。这意味着:
substring(field[@id='0'], 1,3)='MMM'始终为false()
解决方案
替换
<field id="0"><![CDATA[[MMM4443 419280600000]]></field>使用的
<field id="0"><![CDATA[MMM4443 419280600000]]></field>也取代了
<field id="0"><![CDATA[[MMM4443 414390600000]]></field>使用的
<field id="0"><![CDATA[MMM4443 414390600000]]></field>现在,不管使用什么处理器(我家里有9个,可以在其中8个上运行: MSXML3/4、.NET XslCompiledTransform和XslTransform、AltovaXML、Saxon6.5.4、Saxon9.1.05和XQSharp),转换的结果就是我想要的
<Interchange>
<Group>
<Message>
<PriceCatalogue-Lines>
<Line>
<Line-Item>
<LineNumber>1</LineNumber>
<BuyerItemCode>928</BuyerItemCode>
<SubFamily>9</SubFamily>
</Line-Item>
</Line>
<Line>
<Line-Item>
<LineNumber>2</LineNumber>
<BuyerItemCode>439</BuyerItemCode>
<SubFamily>016</SubFamily>
</Line-Item>
</Line>
</PriceCatalogue-Lines>
</Message>
</Group>
</Interchange>我的猜测是,Cooktop的XSLT处理器需要一些配置才能执行document() --请研究可用的文档。
发布于 2011-05-13 03:18:09
Petras,您看到的输出可能意味着XSLT处理器无法找到lookup.xml文件。
由于您使用的是相对网址('lookup.xml'),那么您知道基本网址是什么吗?换句话说,相对于什么?
默认情况下,我认为使用的基URL是样式表的基URL。如果将第二个参数传递给document(),则可以显式设置基URL。例如:
document('lookup.xml', /)将查找相对于输入XML文件的'lookup.xml‘。
你可以通过提供'lookup.xml‘的绝对URL来解决这个问题,或者至少找出问题所在。你为什么不试一试,让我们知道它是否有效。例如。
document('/home/lars/lookup.xml')或
document('file:///c:/temp/lookup.xml')P.S.XML Cooktop一直是一个很棒的软件,但它似乎相当旧,而且现在显然没有维护。当出现小故障时,这就成了一个问题。您可能希望尝试其他仍在维护的XSLT工具,比如OxygenXML或StylusStudio。
https://stackoverflow.com/questions/5966043
复制相似问题