单个XSLT文件是否可以解决这个问题......或者......?

时间:2011-10-29 06:11:14

标签: java xml xslt

以下是我的XML文件 -

<CVs>
  <CV>
    <Name>ABC</Name>
    <Address></Address>
    <Introduction></Introduction>
    <CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
  <CV>
  <CV>
    <Name>XYZ</Name>
    <Address></Address>
    <Introduction></Introduction>
    <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
  <CV>
</CVs>

下面是XSLT文件 - 一个获得想法的简短版本

<xsl:template match="Name">
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;">
<tr><td>
    <xsl:apply-templates/>
    </td></tr>
    </table>
</xsl:template>

<xsl:template match="Experience">
<table align='center' width='800' style="font-family:tahoma; font-size:13pt;">
<tr><td>
    <xsl:apply-templates/>
    </td></tr>
    </table>
</xsl:template>

我使用Java作为前端。要以HTML格式显示输出,我有一个XSLT文件。这个XSLT文件是标准的,即;它显示所有的简历。

现在我要做的是使用一个名为所有候选人名单的ListBox,当点击特定名称时,他的简历应该显示出来。我编写了Java部分,以便将候选者的名称显示在ListBox中。现在在以HTML格式显示所选候选人的CV时遇到一些麻烦。

当前的XSLT文件正在显示所有的CV。那么我是否需要另一个XSLT文件,该文件使用从程序传递的参数并显示其详细信息..?如果是,那么一些帮助如何做到这一点...... ??

提前致谢 - 约翰

2 个答案:

答案 0 :(得分:3)

您可以做的是在XSLT中添加xsl:param并为其提供默认值;比如'全部'。默认情况下,它会显示所有CV个。

如果您需要根据CV显示单个Name,则可以在xsl:param中传递该值(来自您的ListBox),仅显示CV

以下是显示xsl:param s所需的示例xsl:templateCV

  <xsl:param name="pName" select="'All'"/>

  <xsl:template match="CV">
    <xsl:if test="$pName = 'All' or Name = $pName">
      <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>

如果您只想显示XYZ个简历,那么在调用XSLT时,只需使用值XYZ作为pName参数。

答案 1 :(得分:2)

为了让您了解如何做到这一点,这里有一个完整的解决方案,它可以提取所有或只是想要的CV元素(没有HTML格式化,因为这不是与问题相关):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pName" select="'XYZ'"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="CV">
  <xsl:if test="$pName = Name or $pName='*'">
   <xsl:call-template name="identity"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于提供的XML文档(更正为格式良好的文档):

<CVs>
  <CV>
    <Name>ABC</Name>
    <Address></Address>
    <Introduction></Introduction>
    <CompSkills>Java, XSLT, XPATH, XML, Oracle, VB.NET</CompSkills>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
  </CV>
  <CV>
    <Name>XYZ</Name>
    <Address></Address>
    <Introduction></Introduction>
    <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
    <Experience>
      <Profile></Profile>
      <Duration></Duration>
      <Info></Info>
    </Experience>
  </CV>
</CVs>

生成了想要的,正确的(仅提取CV并提取Name XYZ)

<CVs>
   <CV>
      <Name>XYZ</Name>
      <Address/>
      <Introduction/>
      <CompSkills>Java, XSLT, XPATH, XML, JSP, HTML</CompSkills>
      <Experience>
         <Profile/>
         <Duration/>
         <Info/>
      </Experience>
      <Experience>
         <Profile/>
         <Duration/>
         <Info/>
      </Experience>
      <Experience>
         <Profile/>
         <Duration/>
         <Info/>
      </Experience>
   </CV>
</CVs>

<强>解释

必需的名称或“*”必须作为全局参数(在本例中名为pName)从外部传递给转换。阅读您的XSLT处理器文档必须如何完成此操作,因为这取决于实现。