0

I have the following xml

    <City> 
        <StartDate>2015-02-10</StartDate>
        <UpdatedBY>James</UpdatedBY>
        <StudentCode>222</StudentCode>
        <ExamCode>TTED</ExamCode>
     </City>

I need to create a XSLT and transform it to the following XML.

 <School:Region >
     <School:County>
        <School:City>
           <School:StartDate>2015-02-10T00:00:00+08:00</School:StartDate>
           <School:UpdatedBY>James</School:UpdatedBY>
           <School:StudentCode>222</School:StudentCode>
           <School:ExamCode>TTED</School:ExamCode>
        </School:City>
     </School:County>
  </School:Region > 

How do I prefix each element with a prefix 'School'. I have got so for but not sure what I am doing wrong. The

     <xsl:stylesheet version="1.0" xmlns:xsl=""
                        xmlns:max="http://www.w3.org/1999/XSL/Transform/School" >

    <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates select="School"/>
        </xsl:copy>
    </xsl:template>
  <xsl:template match="School" >
    <City>
      <StartDate>
        <xsl:value-of select="Region/County/City/StartDate"/></StartDate>
      <UpdatedBY>
        <xsl:value-of select="Region/County/City/UpdatedBY"/></UpdatedBY>
        <StudentCode><xsl:value-of select="Region/County/City/StudentCode"/></StudentCode>
      <ExamCode>
        <xsl:value-of select="Region/County/City/ExamCode"/></ExamCode>

   </xsl:template>
</xsl:stylesheet>
1
  • Using a namespace URI in a domain that you don't own (in this case www.w3.org) is generally considered both bad manners and bad software engineering. Commented Jun 25, 2015 at 13:52

2 Answers 2

1

How do I prefix each element with a prefix 'School'.

You can try this way :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:School="http://www.w3.org/1999/XSL/Transform/School">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

  <xsl:template match="*">
    <xsl:element name="School:{local-name()}">
      <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
  • The first xsl:template is identity template which, in this particular case, copies all attributes to output XML.
  • The 2nd xsl:template matches all element nodes in source XML, and create corresponding element with prefix School added in the output XML.

Given XML in the question as input, the output is as follow :

<School:City xmlns:School="http://www.w3.org/1999/XSL/Transform/School">
  <School:StartDate>2015-02-10</School:StartDate>
  <School:UpdatedBY>James</School:UpdatedBY>
  <School:StudentCode>222</School:StudentCode>
  <School:ExamCode>TTED</School:ExamCode>
</School:City>
Sign up to request clarification or add additional context in comments.

1 Comment

<xsl:element name="School:{local-name()}"> would be better in case the element already has a prefix.
0

You need to declare the "School:" namespace prefix.

In addition you have the declaration for the xsl namespace wrong. Without the correct declaration, your XSLT processor won't be able to recognize the input as an XSLT stylesheet.

<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:School="YOUR-SCHOOL-NAMESPACE-URI-GOES-HERE">
   <!-- contents -->
</xsl:stylesheet>

Note that each namespace is identified by an URI. Replace the placeholder above with the correct URI for your namespace.

When you have declared the namespace you can just use the prefix in your stylesheet. E.g.

<School:City>
  <!-- contents go here -->
</School:City>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.