单个 XSLT 文件会解决这个问题吗……还是……? [英] Will a Single XSLT file solve this issue..or...?

查看:13
本文介绍了单个 XSLT 文件会解决这个问题吗……还是……?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的 XML 文件 -

Below is my XML File -

<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 文件 - 获取想法的简短版本

below is the XSLT file - a short version to get an idea

<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 文件是一个标准的文件,即;它显示所有的简历.

I am using Java as front-end. To display the output in HTML format I have an XSLT file. This XSLT file is a standard one ie; it displays all the CVs.

现在我要做的是使用一个包含所有候选人姓名的列表框,当点击特定姓名时,只应显示他的简历.我已经对 Java 部分进行了编码,以将候选人的姓名显示到 ListBox 中.现在在以 HTML 格式显示所选候选人的简历时遇到了一些麻烦.

Now what I have to do is use a ListBox with Names of all candidates and when clicked on a particular name ONLY his CV should get displayed. I have coded the Java part to display the names of the candidates into the ListBox. Now have some trouble with displaying the CV of the selected candidate in HTML format.

当前的 XSLT 文件正在显示所有 CV.那么我是否需要另一个使用从程序传递的参数并显示其详细信息的 XSLT 文件..?如果是,那么有关如何执行此操作的一些帮助... ??

The current XSLT file is displaying all the CVs. So Will I need another XSLT file which use parameter passed from the program and display its details..? If yes then some help on how to do this... ??

提前致谢 - 约翰

推荐答案

为了让您了解如何做到这一点,这里有一个 完整 解决方案,可以提取所有或仅需要的 CV 元素(不进行 HTML 格式设置,因为这与问题无关):

To give you an idea how this can be done, here is a complete solution that extracts all or just the wanted CV element (no HTML formatting is done as this isn't relevant to the question):

<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 文档时(更正为格式正确的文档):

When this transformation is applied to the provided XML document (corrected to a well-formed one):

<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>

想要的、正确的(只提取带有Name XYZ的CV):

the wanted, correct (just the CV with Name XYZ is extracted) is produced:

<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 处理器文档必须如何完成,因为这取决于实现.

The wanted name or "*" must be passed externally as a global parameter (in this case named pName) to the transformation. Read your XSLT processor documentation how this must be done, as this is implementation-dependent.

这篇关于单个 XSLT 文件会解决这个问题吗……还是……?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆