我正在尝试访问XSL文档中的内部XML数据。尝试这样做时,Apache Xalan会在使用document('')时抛出java.lang.NullPointerException。
这是XSL源:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:ext="http://exslt.org/common"
xmlns:my="http://example.com/2006/some-data">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<my:params xml:space="preserve">
<pattern>
<old><p></old>
<new>P</new>
</pattern>
<pattern>
<old></p></old>
<new>/P</new>
</pattern>
<pattern>
<old><strong></old>
<new>STRONG</new>
</pattern>
<pattern>
<old></strong></old>
<new>/STRONG</new>
</pattern>
</my:params>
<xsl:variable name="vrtfPats">
<xsl:for-each select="document('')/xsl:stylesheet/my:params/*">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
</xsl:stylesheet>
是否有其他方法可以使用Xalan从XSL文件访问内部数据?
答案 0 :(得分:0)
尝试不声明变量来存储节点并在样式表中使用它们......如:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:ext="http://exslt.org/common"
xmlns:my="http://example.com/2006/some-data">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<my:params xml:space="preserve">
<pattern>
<old><p></old>
<new>P</new>
</pattern>
<pattern>
<old></p></old>
<new>/P</new>
</pattern>
<pattern>
<old><strong></old>
<new>STRONG</new>
</pattern>
<pattern>
<old></strong></old>
<new>/STRONG</new>
</pattern>
</my:params>
<xsl:template match="/">
<test>
<xsl:for-each select="document('')//my:params">
<xsl:apply-templates/>
</xsl:for-each>
</test>
</xsl:template>
<xsl:template match="pattern">
<pattern-match>
<xsl:apply-templates/>
</pattern-match>
</xsl:template>
<xsl:template match="old">
<old-match>
<xsl:apply-templates/>
</old-match>
</xsl:template>
<xsl:template match="new">
<new-match>
<xsl:apply-templates/>
</new-match>
</xsl:template>
</xsl:stylesheet>
得出这个答案:
<test xmlns:my="http://example.com/2006/some-data" xmlns:ext="http://exslt.org/common"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<pattern-match>
<old-match><p></old-match>
<new-match>P</new-match>
</pattern-match>
<pattern-match>
<old-match></p></old-match>
<new-match>/P</new-match>
</pattern-match>
<pattern-match>
<old-match><strong></old-match>
<new-match>STRONG</new-match>
</pattern-match>
<pattern-match>
<old-match></strong></old-match>
<new-match>/STRONG</new-match>
</pattern-match>
</test>