转换后以文本而不是xml节点输出

时间:2009-10-12 06:20:30

标签: xslt

我的问题是在执行xlst文件后,我在一行中获得文本输出,但不是根据需要在xml中。我的xml和xslt文件如下。

<root>
  <Jobs Found="10" Returned="50"> 
  <Job ID="8000000" PositionID="600002"> 
  <Title>Development Manager</Title>  
  <Summary> 
     <![CDATA[ An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.&#160;&#160; My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t 
  ]]>  
  </Summary> 
  <DateActive Date="2009-10-06T19:36:43-05:00">10/6/2009</DateActive>  
  <DateExpires Date="2009-11-05T20:11:34-05:00">11/5/2009</DateExpires>  
  <DateUpdated Date="2009-10-06 20:12:00">10/6/2009</DateUpdated>  
  <Location> 
    <Country>xxxx</Country>  
    <State>xxx</State>  
    <City>xxx</City>  
    <PostalCode>xxx</PostalCode>  
  </Location> 
  <CompanyName>abc Technology</CompanyName>  
  <BuilderFields />  
  <DisplayOptions />  
  <AddressType>1234</AddressType>  
  </Job> 
      </Jobs>
</root>

XSLT样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:output method="xml" indent="yes" media-type="application/xml" 
              cdata-section-elements="Summary"/> 

   <!-- default: copy everything using the identity transform --> 
   <xsl:template match="@*|node()"> 
      <xsl:copy> 
         <xsl:apply-templates select="@*|node()"/> 
      </xsl:copy> 
   </xsl:template> 

   <!-- override: for Location and Salary nodes, just process the children --> 
   <xsl:template match="Location|Salary"> 
      <xsl:apply-templates select="node()"/> 
   </xsl:template> 

   <!-- override: for selected elements, convert attributes to elements --> 
   <xsl:template match="Jobs/@*|Job/@*"> 
      <xsl:element name="{name()}"> 
         <xsl:value-of select="."/> 
      </xsl:element> 
   </xsl:template> 

   <!-- override: for selected elements, remove attributes --> 
   <xsl:template match="DateActive/@*|DateExpires/@*|DateUpdated/@*"/> 
</xsl:stylesheet> 

文本中的当前输出是:

492 50 83000003 61999998市场领先的公司新成立的角色具有责任感,可见性和优势的高调职位机会必须具备扎实的BA技能,在SDLC环境中磨练市场领先的公司具有新创造的角色具有责任感,可视性和优势的高调职位机会必须拥有扎实的BA技能在SDLC环境中磨练我的客户是市场领导者,他们将继续从强项出发10/5/2009 11/4/2009 10/5/2009澳大利亚新南威尔士州悉尼2000技能任务90,000.00 120,000.00每年澳元6

这是我想要的xml。请帮助我找到解决方案。

3 个答案:

答案 0 :(得分:1)

在XSLT文件的顶部是否有这样的行?

<xsl:output method="xml" indent="yes"/>

定义输出格式是什么 - “text”是默认值,“html”和“xml”是其他选项。

我不知道你在做什么,但是当我在提供的示例XML文件上运行你的XSLT文件时,我将其作为输出:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Jobs><Found>10</Found><Returned>50</Returned>
    <Job><ID>8000000</ID><PositionID>600002</PositionID>
      <Title>Development Manager</Title>
      <Summary>
         An experienced Development Manager with previous experience leading a small to mid-size team of developers in a Java/J2EE environment. A hands on role, you will be expected to manage and mentor a team of developers working on a mix of greenfield and maintenance projects.&amp;#160;&amp;#160; My client, a well known investment bank, requires an experienced Development Manager to join their core technology team. This t 

      </Summary>
      <DateActive>10/6/2009</DateActive>
      <DateExpires>11/5/2009</DateExpires>
      <DateUpdated>10/6/2009</DateUpdated>

        <Country>xxxx</Country>
        <State>xxx</State>
        <City>xxx</City>
        <PostalCode>xxx</PostalCode>

      <CompanyName>abc Technology</CompanyName>
      <BuilderFields />
      <DisplayOptions />
      <AddressType>1234</AddressType>
    </Job>
  </Jobs>
</root>

马克

答案 1 :(得分:1)

我怀疑您正在浏览器中观看转换结果。

转换本身运行良好,但浏览器显示XML的纯文本(因为它默认需要HTML内容,并忽略它无法识别的任何标记,仅显示其文本内容)。

尝试media-type="text/xml",看看是否有任何区别。如果没有,请不要让浏览器显示混淆你 - XSLT没有任何问题。您应该使用另一个XSLT处理器来确认/调试XSLT。

答案 2 :(得分:0)

您可能会写出xml节点的内部文本,而不是在其中一个节点中调用apply-templates。我找不到你附加的xsl,所以不容易猜到。但发布xslt,我会告诉你。

相关问题