【问题标题】:After transformation getting output in text instead of xml nodes转换后以文本而不是 xml 节点输出
【发布时间】:2009-10-12 06:20:30
【问题描述】:

我的问题是在执行 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 市场领先的公司,新设立的职位 具有责任、可见性和机会的高调职位 必须具有在 SDLC 环境中磨练的扎实 BA 技能 市场领先的公司,新设立的职位 具有责任的高调职位,可见性和机会必须在 SDLC 环境中磨练扎实的 BA 技能 我的客户是市场领导者,他将继续从 2009 年 10 月 5 日 2009 年 11 月 4 日 2009 年 10 月 5 日到 2009 年澳大利亚新南威尔士州悉尼 2000 年技能探索 90,000.00 120,000.00 每年 6 澳元

我想要在 xml 中的这个输出。请帮助我找到解决方案。

【问题讨论】:

  • 我建议你阅读 StackOverflow 的标记。您的 XML 不可见。
  • 您的 XML 无效 - 标签永远不会关闭.....

标签: xslt


【解决方案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>

马克

【讨论】:

  • 写在xslt文件的顶部,请自行在问题中找到xslt文件内容
【解决方案2】:

我怀疑你正在浏览器中查看转换结果。

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • xslt文件内容粘贴在问题下方,请找到。
    猜你喜欢
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多