2014-08-27 57 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