Xalan redirect:write , use either of two element values to create name of new .xml file depending on null values
So I have the following code:
<redirect:write select="concat('..\\folder\\,string(filename),'.xml')">
Where "filename" is a tag in the xml source. My problem occurs when filename is null or blank. And this is the case for several of the xml filename tags. So what I am trying to implement is a checking method. This is what I have done:
<xsl-if test = "filename != ''">
<xsl:variable name = "tempName" select = "filename" />
</xsl-if>
<xsl-if test ="filename = ''">
<xsl:variable name = "tempName" select = "filenameB"/>
</xsl-if>
<redirect:write select="concat开发者_开发知识库('..\\folder\\,string($tempName),'.xml')">
I seem to be getting NPEs when I compile my Java code, saying the Variable not resolvable: tempName
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:redirect="my:redirect"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="filename">
<xsl:variable name="tempName">
<xsl:choose>
<xsl:when test="text()">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>filenameB</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<redirect:write select="..\\folder\\{$tempName}.xml"/>
</xsl:template>
</xsl:stylesheet>
when applied to this XML document:
<t xmlns:redirect="my:redirect">
<filename>Z</filename>
<filename/>
</t>
produces the wanted, correct result:
<t xmlns:redirect="my:redirect">
<redirect:write select="..\\folder\\Z.xml" />
<redirect:write select="..\\folder\\filenameB.xml" />
</t>
Do note: Whenever the value of a variable is established based on a condition, this condition is implemented using <xsl:choose>
inside the body of the variable.
精彩评论