2

Here is my problem.

I have some pre build XSLT with call template, Template code in another file I can edit content of template code , but not main XSLT.

My Current Code

School_College.xsl (I can not edit this)

<xsl:stylesheet version="1.0"
            xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:ns0="http://www.myschool.org"
            xmlns:ora="http://schemas.oracle.com/xpath/extension"
            xmlns:ns1="http://www.mycollege.org"

         <xsl:import href="school_college_custom.xsl"></xsl:import>

 <xsl:template match="/">
   <ns1:College>
     <ns1:Batch>
       <ns1:StudentsCount>
         <xsl:value-of select="/ns0:School/ns0:Class/ns0:NoOfStudents"/>
       </ns1:StudentsCount>
       <ns1:TeachersCount>
          <xsl:value-of select="/ns0:School/ns0:Class/ns0:NoOfTeachers"/>
       </ns1:TeachersCount>
       <ns1:BatchStartTime>
          <xsl:value-of select="/ns0:School/ns0:Class/ns0:ClassStartTime"/>
       </ns1:BatchStartTime>

       <xsl:call-template name="batch_ext"></xsl:call-template>

     </ns1:Batch>
   </ns1:College>
</xsl:template>

school_college_custom.xsl (I can modify this , imported & called from above)

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.mycollege.org">

  <xsl:template name="batch_ext">
        <ns1:BatchEndTime>
          <xsl:value-of select="'5PM'"/>
        </ns1:BatchEndTime>
        <ns1:BreakTime>
          <xsl:value-of select="'1PM'"/>
        </ns1:BreakTime>
  </xsl:template>
</xsl:stylesheet>

Source XML:

<?xml version="1.0" encoding="UTF-8" ?>
  <School xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.myschool.org xsd/school.xsd"
    xmlns="http://www.myschool.org">
   <Class>
     <NoOfStudents>10</NoOfStudents>
     <NoOfTeachers>2</NoOfTeachers>
     <ClassStartTime>9AM</ClassStartTime>
  </Class>
 </School>

Target XML:(current)

<?xml version="1.0" encoding="UTF-8" ?>
<College xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.mycollege.org ../xsd/college.xsd"
     xmlns="http://www.mycollege.org">
  <Batch>
    <StudentsCount>10</StudentsCount>
    <TeachersCount>2</TeachersCount>
    <BatchStartTime>9AM</BatchStartTime>
    <BatchEndTime>5PM</BatchEndTime>
    <BreakTime>1PM</BreakTime>
  </Batch>
</College>

Is there any way to update BatchStartTime to 10AM(any value). but i need to modify school_college_custom.xsl only. if add "BatchStartTime" element in template(like below) it is creating duplicate element

my code : this is not working (creating duplicate node)

<?xml version="1.0" encoding="windows-1252" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.mycollege.org">

  <xsl:template name="batch_ext">
        <ns1:BatchStartTime>
          <xsl:value-of select="'10AM'"/>
        </ns1:BatchStartTime>
        <ns1:BatchEndTime>
          <xsl:value-of select="'5PM'"/>
        </ns1:BatchEndTime>
        <ns1:BreakTime>
          <xsl:value-of select="'1PM'"/>
        </ns1:BreakTime>
  </xsl:template>
</xsl:stylesheet>

Target XML:(Expected, I need output like this)

<?xml version="1.0" encoding="UTF-8" ?>
<College xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.mycollege.org ../xsd/college.xsd"
     xmlns="http://www.mycollege.org">
  <Batch>
    <StudentsCount>10</StudentsCount>
    <TeachersCount>2</TeachersCount>
    <BatchStartTime>10AM</BatchStartTime>
    <BatchEndTime>5PM</BatchEndTime>
    <BreakTime>1PM</BreakTime>
  </Batch>
</College>

Target XML:(Actual output from above code)

<?xml version="1.0" encoding="UTF-8" ?>
<College xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.mycollege.org ../xsd/college.xsd"
     xmlns="http://www.mycollege.org">
  <Batch>
    <StudentsCount>10</StudentsCount>
    <TeachersCount>2</TeachersCount>
    <BatchStartTime>9AM</BatchStartTime>
    <BatchStartTime>10AM</BatchStartTime>
    <BatchEndTime>5PM</BatchEndTime>
    <BreakTime>1PM</BreakTime>
  </Batch>
</College>

Please give some solution. Thanks in advance.

1 Answers1

0

No, you can't modify the nodes that have already been added to the result tree before your custom template was called. If the main (importing) XSLT were to use xsl:apply-templates instead of xsl:value-of to get the values to insert into the result then you would be able to define an

<xsl:template match="ns0:ClassStartTime">10AM</xsl:template>

to override the default template for this element, but with the main XSLT written as it is there's nothing you can do.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183