如果我正确理解了这个问题,解决方案只需将此模板添加到您当前的 xsl:
<xsl:template match="workphone">
<xsl:if test="$office='Chicago'">(773) </xsl:if>
<xsl:if test="$office='Orland'">(708) </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
用您在原始帖子中提到的变量替换此“$office”变量。
编辑:在这里,您有一个完整的转换,其中以前的模板应用于示例输入 XML(顺便说一句:尝试遵循 Jim Garrison 的建议。在以后的帖子中发布完整的输入/输出 XML 而不是其中的一部分,您将得到准确的响应第一次尝试)。
输入:
<Office code="Chicago">
<Employee id="1">
<hithighlightedproperties>
<preferredname>Nigro, Brandon L.</preferredname>
<yomidisplayname></yomidisplayname>
<department>Information Technology</department>
<workphone>555-5555</workphone>
<officenumber>John Academic Center</officenumber>
</hithighlightedproperties>
</Employee>
</Office>
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:variable name="office" select="Office/@code"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="Office">
<html>
<body>
<xsl:apply-templates select="Employee"/>
</body>
</html>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="Employee">
<xsl:variable name="haswph" select="string-length(hithighlightedproperties/workphone) > 0"/>
<xsl:variable name="hasonum" select="string-length(hithighlightedproperties/officenumber)> 0"/>
<p>Employee <xsl:value-of select="@id"/> Properties</p>
<ul>
<li id="Name"><xsl:value-of select="hithighlightedproperties/preferredname"/></li>
<xsl:if test="string-length(hithighlightedproperties/yomidisplayname)">
<li id="DisplayNameField">
<xsl:apply-templates select="hithighlightedproperties/yomidisplayname"/>
</li>
</xsl:if>
<li id="DepartmentField"><xsl:value-of select="hithighlightedproperties/department"/></li>
<xsl:if test="$haswph">
<li id="PhoneField">
<xsl:apply-templates select="hithighlightedproperties/workphone"/>
</li>
</xsl:if>
<xsl:if test="$hasonum">
<li id="OfficeField">
<xsl:apply-templates select="hithighlightedproperties/officenumber"/>
</li>
</xsl:if>
</ul>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="workphone">
<xsl:if test="$office='Chicago'">(773) </xsl:if>
<xsl:if test="$office='Orland'">(708) </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出:
<html>
<body>
<p>Employee 1 Properties</p>
<ul>
<li id="NameField">Nigro, Brandon L.</li>
<li id="DepartmentField">Information Technology</li>
<li id="PhoneField">(773) 555-5555</li>
<li id="OfficeField">John Academic Center</li>
</ul>
</body>
</html>
但是,我不鼓励您使用这种“变量 + if”方法,并尝试使用干净的 xsl pushy 样式(这会达到完全相同的结果):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="Office">
<html>
<body>
<xsl:apply-templates select="Employee"/>
</body>
</html>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="Employee">
<p>Employee <xsl:value-of select="@id"/> Properties</p>
<ul>
<xsl:apply-templates select="hithighlightedproperties/preferredname" mode="li">
<xsl:with-param name="id" select="'NameField'"/>
</xsl:apply-templates>
<xsl:apply-templates select="hithighlightedproperties/yomidisplayname" mode="li">
<xsl:with-param name="id" select="'DisplayNameField'"/>
</xsl:apply-templates>
<xsl:apply-templates select="hithighlightedproperties/department" mode="li">
<xsl:with-param name="id" select="'DepartmentField'"/>
</xsl:apply-templates>
<xsl:apply-templates select="hithighlightedproperties/workphone" mode="li">
<xsl:with-param name="id" select="'PhoneField'"/>
</xsl:apply-templates>
<xsl:apply-templates select="hithighlightedproperties/officenumber" mode="li">
<xsl:with-param name="id" select="'OfficeField'"/>
</xsl:apply-templates>
</ul>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="workphone">
<xsl:variable name="office" select="../../../@code"/>
<xsl:if test="$office='Chicago'">(773) </xsl:if>
<xsl:if test="$office='Orland'">(708) </xsl:if>
<xsl:value-of select="."/>
</xsl:template>
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<xsl:template match="*[string-length(.)>0]" mode="li">
<xsl:param name="id" select="local-name()"/>
<li id="{$id}">
<xsl:apply-templates select="."/>
</li>
</xsl:template>
</xsl:stylesheet>