2014-08-27 58 views
0

我对xsl技术非常陌生。我试图做的是分裂以下文件:无法使用xsl在springsource中分割xml文件IDE

<?xml version="1.0" encoding="UTF-8"?> 
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata"> 
    <fields> 
     <fullName>StageName</fullName> 
     <picklist> 
      <picklistValues> 
       <fullName>Prospecting/Needs/Qualification</fullName> 
       <closed>false</closed> 
       <default>false</default> 
       <forecastCategory>Pipeline</forecastCategory> 
       <probability>25</probability> 
       <won>false</won> 
      </picklistValues> 
      <picklistValues> 
       <fullName>Proposal/Price Quote</fullName> 
       <closed>false</closed> 
       <default>false</default> 
       <forecastCategory>Pipeline</forecastCategory> 
       <probability>40</probability> 
       <won>false</won> 
      <sorted>false</sorted> 
     </picklist> 
     <trackFeedHistory>true</trackFeedHistory> 
     <trackTrending>false</trackTrending> 
     <type>Picklist</type> 
    </fields> 
    <fields> 
     <fullName>Status__c</fullName> 
     <externalId>false</externalId> 
     <label>Status</label> 
     <required>false</required> 
     <trackHistory>false</trackHistory> 
     <trackTrending>false</trackTrending> 
     <type>Text</type> 
     <unique>false</unique> 
    </fields> 
</CustomObject> 

到两个不同的文件,每个场/全名的名字命名 - 后者的元素。所以基本上我想第一个文件:“StageName.xml”:

<fields> 
      <fullName>StageName</fullName> 
      <picklist> 
       <picklistValues> 
        <fullName>Prospecting/Needs/Qualification</fullName> 
        <closed>false</closed> 
        <default>false</default> 
        <forecastCategory>Pipeline</forecastCategory> 
        <probability>25</probability> 
        <won>false</won> 
       </picklistValues> 
       <picklistValues> 
        <fullName>Proposal/Price Quote</fullName> 
        <closed>false</closed> 
        <default>false</default> 
        <forecastCategory>Pipeline</forecastCategory> 
        <probability>40</probability> 
        <won>false</won> 
       <sorted>false</sorted> 
      </picklist> 
      <trackFeedHistory>true</trackFeedHistory> 
      <trackTrending>false</trackTrending> 
      <type>Picklist</type> 
     </fields> 

和第二个文件:“Status__c.xml”:

<fields> 
      <fullName>Status__c</fullName> 
      <externalId>false</externalId> 
      <label>Status</label> 
      <required>false</required> 
      <trackHistory>false</trackHistory> 
      <trackTrending>false</trackTrending> 
      <type>Text</type> 
      <unique>false</unique> 
     </fields> 

我已经写了下面的小脚本,基于问题 - 答案在计算器:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
       xmlns:redirect="http://xml.apache.org/xalan/redirect" 
       extension-element-prefixes="redirect"> 

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

    <xsl:template match="/root"> 
     <xsl:apply-templates/> 
     <xsl:for-each select="fields"> 
      <redirect:write file="file_{@id}-output.xml"> 
        <xsl:copy-of select="fields"/> 
      </redirect:write> 
     </xsl:for-each> 
    </xsl:template> 


</xsl:stylesheet> 

和我所得到的是下面的唯一文件: “Opportunity.out.xml”:

<?xml version="1.0" encoding="UTF-8"?> 


     StageName 


       Prospecting/Needs/Qualification 
       false 
       false 
       Pipeline 
       25 
       false 


       Proposal/Price Quote 
       false 
       false 
       Pipeline 
       40 
       false 

      false 

     true 
     false 
     Picklist 


     Status__c 
     false 
     Status 
     false 
     false 
     false 
     Text 
     false 

我正在运行split.xsl文件,通过右键单击 - >运行为--->在SpringSource中运行XSL转换。

对于庞大的问题,我很抱歉,但我真的不知道从哪里开始解决这个问题。

预先感谢您。

回答

0

您需要在输入XML中考虑名称空间,方法是在样式表中声明它的前缀,然后使用该前缀限定元素名称。你也已经发布了样品的根不是root而是CustomObject所以alltoghether我们需要

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
       xmlns:redirect="http://xml.apache.org/xalan/redirect" 
       xmlns:df="http://soap.sforce.com/2006/04/metadata" 
       extension-element-prefixes="redirect" 
       exclude-result-prefixes="df redirect"> 

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

<xsl:template match="/df:CustomObject"> 
    <xsl:apply-templates/> 
    <xsl:for-each select="df:fields"> 
    <redirect:write file="file_{df:fullName}-output.xml"> 
     <xsl:copy-of select="."/> 
    </redirect:write> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

当我运行以命令行的Xalan 2.7的Java它生成的输出文件file_StageName-output.xmlfile_Status__c-output.xml

所以你可能宁愿要<redirect:write file="{df:fullName}-output.xml">。至于如何使用IDE来处理问题,请使用与IDE相关的标签标记问题,以便希望熟悉此问题的人员能够提供帮助。

+0

你好马丁,谢谢你的回答。我试过这个解决方案,但我仍然获得了我发布的初始输出。你认为这可能与springource配置有关吗? – Antigoni 2014-08-28 08:17:00

+0

@Antigoni,我已经编辑了答案,并修正了一个匹配模式,以便与您提供的样本一起工作(这也需要进行更正以使其成形良好)。 – 2014-08-28 09:13:04

+0

好的@Martin,再次感谢。最后两个简单问题:我如何检查我的Xalan版本,以及2.您在命令行输入什么命令以从那里运行它?再次感谢您的帮助。 – Antigoni 2014-08-28 12:55:38