我的培训师的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 :(得分: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>