XSLT变量无法识别求助:样式表无法读取已定义变量
XSLT变量无法读取的问题解决
你的问题出在直接在文本中写$customer,XSLT不会自动解析这种写法,必须使用XSLT的变量引用语法。
原代码:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:template match="/JDF"> <xsl:variable name="customer" select="/JDF/ResourcePool[1]/CustomerInfo[1]/@CustomerID"/> <job> <jobInfo>$customer</jobInfo> </job> </xsl:template> </xsl:stylesheet>
错误运行结果:
<?xml version="1.0" encoding="UTF-8"?> <job> <jobInfo>$customer</jobInfo> </job>
修正方案:
要输出变量值,必须用<xsl:value-of select="$customer"/>替代直接写$customer。另外,由于模板已匹配/JDF,变量的select路径可以简化(无需再从根节点开始)。
修正后的代码:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:template match="/JDF"> <xsl:variable name="customer" select="ResourcePool[1]/CustomerInfo[1]/@CustomerID"/> <job> <jobInfo><xsl:value-of select="$customer"/></jobInfo> </job> </xsl:template> </xsl:stylesheet>
如果你的XML输入中存在对应的CustomerID属性,运行后就能正确输出该属性的值,而非$customer字符串。
内容的提问来源于stack exchange,提问作者ItalianDadx2
相关产品推荐
相关产品推荐

