Добавить дочерний узел через XLST в SSLS

извините, опять я.

У меня есть 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" 
     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>

Есть идеи, что я делаю неправильно?

1 ответ

  1. AFAICT, вы хотите сделать:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    >
    <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>