2017-01-16 31 views
0

對不起,我再次。通過SSLS中的XLST添加子節點

我從我們教練的Captivate的測驗這樣的XML文件:

我已經通過XLST管理(從這裏社區的幫助)的屬性元素轉換,我想接下來就是做從<CompanyName><TotalQuestions>以上的元素並將它們包含在一個新節點中; <userdata>

這是我有:

<Course> 
    <CompanyName value='Hanover'/> 
    <DepartmentName value='ICT'/> 
    <CourseName value='TEST'/> 
    <LearnerName value='Paul Wilson'/> 
    <LearnerID value='05757'/> 
    <LessonName value='ICT Literacy Test'></LessonName> 
    <QuizAttempts value='1'></QuizAttempts> 
    <TotalQuestions value='26'></TotalQuestions> 
    <Result><CoreData><Status value='completed'></Status> 
    <Location value='30'></Location> 
    <RawScore value='100'></RawScore> 
    <MaxScore value='100'></MaxScore> 
    <MinScore value='0'></MinScore> 
    <SessionTime value='undefined'></SessionTime> 
    </CoreData> 
    <InteractionData><Interactions><Date value='2016/11/23'></Date> 
    <InteractionTime value='11/23/2016T8:40:32'></InteractionTime> 
    <InteractionID value='WN_Open_File'></InteractionID> 
    <ObjectiveID value='Quiz_201611414227'></ObjectiveID> 
    <InteractionType value='choice'></InteractionType> 
    <CorrectResponse value='1'></CorrectResponse> 
    <StudentResponse value='1'></StudentResponse> 
    <Result value='C'></Result> 
    <Weight value='1'></Weight> 
    <Latency value='1916'></Latency> 
    <Attempt value='1'></Attempt> 
    </Interactions> 
    <Interactions><Date value='2016/11/23'></Date> 
    <InteractionTime value='11/23/2016T8:40:43'></InteractionTime> 
    <InteractionID value='WN_Close_Window'></InteractionID> 
    <ObjectiveID value='Quiz_201611414227'></ObjectiveID> 
    <InteractionType value='choice'></InteractionType> 
    <CorrectResponse value='1'></CorrectResponse> 
    <StudentResponse value='1'></StudentResponse> 
    <Result value='C'></Result> 
    <Weight value='1'></Weight> 
    <Latency value='10889'></Latency> 
    <Attempt value='1'></Attempt> 
    </Interactions> 
    <Interactions><Date value='2016/11/23'></Date> 
    <InteractionTime value='11/23/2016T8:44:41'></InteractionTime> 
    <InteractionID value='MO_Address'></InteractionID> 
    <ObjectiveID value='Quiz_2016103113211'></ObjectiveID> 
    <InteractionType value='choice'></InteractionType> 
    <CorrectResponse value='B'></CorrectResponse> 
    <StudentResponse value='B'></StudentResponse> 
    <Result value='C'></Result> 
    <Weight value='1'></Weight> 
    <Latency value='6601'></Latency> 
    <Attempt value='1'></Attempt> 
    </Interactions> 
    </InteractionData> 
    </Result> 
    </Course> 

這就是我想要的:

<?xml version="1.0" encoding="utf-8"?> 
<Course> 
    <UserData> 
    <CompanyName>Hanover</CompanyName> 
    <DepartmentName>ICT</DepartmentName> 
    <CourseName>TEST</CourseName> 
    <LearnerName>Paul Wilson</LearnerName> 
    <LearnerID>05757</LearnerID> 
    <LessonName>ICT Literacy Test</LessonName> 
    <QuizAttempts>1</QuizAttempts> 
    <TotalQuestions>26</TotalQuestions> 
    </UserData> 
    <Result> 
     <CoreData> 
     <Status>completed</Status> 
     <Location>30</Location> 
     <RawScore>100</RawScore> 
     <MaxScore>100</MaxScore> 
     <MinScore>0</MinScore> 
     <SessionTime>undefined</SessionTime> 
     </CoreData> 
     <InteractionData> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:40:32</InteractionTime> 
      <InteractionID>WN_Open_File</InteractionID> 
      <ObjectiveID>Quiz_201611414227</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>1</CorrectResponse> 
      <StudentResponse>1</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>1916</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:40:43</InteractionTime> 
      <InteractionID>WN_Close_Window</InteractionID> 
      <ObjectiveID>Quiz_201611414227</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>1</CorrectResponse> 
      <StudentResponse>1</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>10889</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:44:41</InteractionTime> 
      <InteractionID>MO_Address</InteractionID> 
      <ObjectiveID>Quiz_2016103113211</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>B</CorrectResponse> 
      <StudentResponse>B</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>6601</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     </InteractionData> 
    </Result> 

</Course> 

我已經嘗試使用下面的XSLT代碼:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="Course"> 
     <xsl:copy> 
      <UserData> 
       <xsl:apply-templates select="@*|node()"/> 
      </UserData> 
     </xsl:copy> 
    </xsl:template> 


    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 


    <xsl:template match="*[@value]"> 
     <xsl:copy> 
      <xsl:value-of select="@value"/> 
     </xsl:copy> 
    </xsl:template> 



</xsl:stylesheet> 

但給我這個:

<?xml version="1.0" encoding="utf-8"?> 
<Course> 
    <UserData> 
    <CompanyName>Hanover</CompanyName> 
    <DepartmentName>ICT</DepartmentName> 
    <CourseName>TEST</CourseName> 
    <LearnerName>Paul Wilson</LearnerName> 
    <LearnerID>05757</LearnerID> 
    <LessonName>ICT Literacy Test</LessonName> 
    <QuizAttempts>1</QuizAttempts> 
    <TotalQuestions>26</TotalQuestions> 
    <Result> 
     <CoreData> 
     <Status>completed</Status> 
     <Location>30</Location> 
     <RawScore>100</RawScore> 
     <MaxScore>100</MaxScore> 
     <MinScore>0</MinScore> 
     <SessionTime>undefined</SessionTime> 
     </CoreData> 
     <InteractionData> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:40:32</InteractionTime> 
      <InteractionID>WN_Open_File</InteractionID> 
      <ObjectiveID>Quiz_201611414227</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>1</CorrectResponse> 
      <StudentResponse>1</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>1916</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:40:43</InteractionTime> 
      <InteractionID>WN_Close_Window</InteractionID> 
      <ObjectiveID>Quiz_201611414227</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>1</CorrectResponse> 
      <StudentResponse>1</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>10889</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     <Interactions> 
      <Date>2016/11/23</Date> 
      <InteractionTime>11/23/2016T8:44:41</InteractionTime> 
      <InteractionID>MO_Address</InteractionID> 
      <ObjectiveID>Quiz_2016103113211</ObjectiveID> 
      <InteractionType>choice</InteractionType> 
      <CorrectResponse>B</CorrectResponse> 
      <StudentResponse>B</StudentResponse> 
      <Result>C</Result> 
      <Weight>1</Weight> 
      <Latency>6601</Latency> 
      <Attempt>1</Attempt> 
     </Interactions> 
     </InteractionData> 
    </Result> 
    </UserData> 
</Course> 

任何想法我做錯了什麼?

+0

請編輯您的問題,並添加了預期的結果。將示例最小化爲僅顯示問題所需的內容也很有用 - 請參閱:[mcve]。 –

+0

我會盡力而爲。我以前遇到過性格限制。 –

回答

0

AFAICT,你想做的事:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="Course"> 
    <xsl:copy> 
     <UserData> 
      <xsl:apply-templates select="*[not(self::Result)]"/> 
     </UserData> 
     <xsl:apply-templates select="Result"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*[@value]"> 
    <xsl:copy> 
     <xsl:value-of select="@value"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

這很好,一旦我註釋掉了,它就在SSIS中工作。 非常感謝。 –

相關問題