Reputation: 1027
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>
Upvotes: 0
Views: 397
Reputation: 89285
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>
xsl:template
is identity template which, in this particular case, copies all attributes to output XML.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>
Upvotes: 1
Reputation: 10188
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>
Upvotes: 0