xsl-list
[Top] [All Lists]

Re: [xsl] Changing namespaces

2011-08-16 13:25:44
David,

Finally I got the right thing as follows. However can you suggest any
technical references for the basic or core concepts of XSLT? as still
there are many things I am not clear with as to how XSLT creates the
output. Many thanks.

<xsl:stylesheet
   version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:olddef="old-default-namespace"
   xmlns="new-default-namespace"
   xmlns:oldOC="old-oc-namespace-uri"
   xmlns:OC="new-oc-namespace-uri"
    xmlns:OCRules="oc-rules-uri"
   exclude-result-prefixes="olddef oldOC">

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

<xsl:template name="removeOCExtnElmnt" priority="6" match="//*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template name="removeOCExtnAttrib" priority="5" match="//@*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template name="updateODMVersion" priority="7" match="@ODMVersion" >
        <xsl:attribute name="ODMVersion">1.2</xsl:attribute>
</xsl:template>
                
<xsl:template name="copyOC1.2Elmnt" priority="8"
match="oldOC:MultiSelectList | oldOC:MultiSelectListRef |
oldOC:MultiSelectListItem" >
        <xsl:element name="OpenClinica:{local-name()}"
namespace="new-oc-namespace-uri">
                <xsl:apply-templates select="@*,node()"/>
         </xsl:element>
</xsl:template>
        
<xsl:template name="copyOC1.2Attrib" priority="9"
match="@*:StudySubjectID | @*:UniqueIdentifier | @*:Status
| @*:DateOfBirth | @*:Sex | @*:StudyEventLocation | @*:StartDate
| @*:SubjectAgeAtEvent | @*:Version | @*:InterviewerName
| @*:InterviewDate | @*:Status" >
        <xsl:attribute name="OpenClinica:{local-name()}"
namespace="new-oc-namespace-uri">
                <xsl:value-of select="."/>
        </xsl:attribute>        
</xsl:template>

<xsl:template name="removeOCRulesElmnts" priority="10" match="//*[
namespace-uri()='oc-rules-uri' ]" ></xsl:template>

<xsl:template name="removeOCRulesAttribs" priority="11" match="//@*[
namespace-uri()='oc-rules-uri' ]" ></xsl:template>

<xsl:template name="changeNSTo1.2" priority="12" match="olddef:*" >
        <xsl:element name="{local-name()}" namespace="new-default-namespace">
                <xsl:namespace name="OpenClinica" 
select="'new-oc-namespace-uri'"/>     
                <xsl:apply-templates select="@*,node()"/>
         </xsl:element>
</xsl:template>

</xsl:stylesheet>


- Pradnya





On Tue, Aug 16, 2011 at 1:23 PM, Pradnya Gawade
<pradnya(_dot_)gawade7(_at_)gmail(_dot_)com> wrote:
Hi,

I am having the same issue of xmlns, xmlns:OC="..." getting displayed
in the individual elements after combining the 2 stylesheets like
following:

<OC:MultiSelectList xmlns:OC="old-oc-namespace-uri" ID="MSL_176"
Name="multiselect" DataType="text" ActualDataType="text">

<SubjectData xmlns="old-default-namespace"
xmlns:OC="old-oc-namespace-uri" SubjectKey="SS_PGCOMPLE"
OC:StudySubjectID="pgcomplete">

any suggestions please?

I think there is no need to have 2 copy templates after I combine but
cannot imagine how the templates in my first stylesheet and second
stylesheet can work together. I am putting the priority for templates
from your stylesheet as lower number expecting it to get executed at
the end. Combined stylesheet looks like:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:olddef="old-default-namespace"
  xmlns="new-default-namespace"
  xmlns:oldOC="old-oc-namespace-uri"

  xmlns:OC="new-oc-namespace-uri"
   xmlns:OCRules="oc-rules-uri"
  exclude-result-prefixes="olddef oldOC"

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

<xsl:template name="removeOCExtnElmnt" priority="6" match="//*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template name="removeOCExtnAttrib" priority="5" match="//@*[
namespace-uri()='old-oc-namespace-uri' ]" ></xsl:template>

<xsl:template priority="7" match="@ODMVersion" >
       <xsl:attribute name="ODMVersion">1.2</xsl:attribute>
</xsl:template>

<xsl:template priority="8" match="oldOC:MultiSelectList |
oldOC:MultiSelectListRef | oldOC:MultiSelectListItem" >
       <xsl:element name="{name()}" namespace="{namespace-uri()}" >
               <xsl:copy-of select="@*"/>
               <xsl:apply-templates/>
       </xsl:element>

</xsl:template>

<xsl:template priority="9" match="//*[@oldOC:StudySubjectID |
@oldOC:UniqueIdentifier | @oldOC:Status
| @oldOC:DateOfBirth | @oldOC:Sex | @oldOC:StudyEventLocation |
@oldOC:StartDate
| @oldOC:SubjectAgeAtEvent | @oldOC:Version | @oldOC:InterviewerName
| @oldOC:InterviewDate | @oldOC:Status]" >
       <xsl:element name="{name()}" namespace="{namespace-uri()}">
               <xsl:copy-of select="@*" copy-namespaces="no"/>
               <xsl:apply-templates/>
       </xsl:element>
</xsl:template>

<xsl:template name="removeOCRulesElmnts" priority="10" match="//*[
namespace-uri()='oc-rules-uri' ]" ></xsl:template>
<xsl:template name="removeOCRulesAttribs" priority="11" match="//@*[
namespace-uri()='oc-rules-uri' ]" ></xsl:template>

<!-- ++++++++++  code to change the namespaces in root tag ++++++++++ -->

<xsl:template name="first" match="@*|*" priority="12" mode="part2">

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

<xsl:template name="second" match="olddef:*" priority="4">
 <xsl:element name="{local-name()}" namespace="new-default-namespace">
 <xsl:namespace name="OC" select="'new-oc-namespace-uri'"/>

 <xsl:apply-templates select="@*,node()"/>
 </xsl:element>
</xsl:template>

<xsl:template name="third" match="oldOC:*" priority="3">
 <xsl:element name="OC:{local-name()}" namespace="new-oc-namespace-uri">

 <xsl:apply-templates select="@*,node()"/>
 </xsl:element>
</xsl:template>

<xsl:template name="fourth" match="@oldOC:*" priority="2">
 <xsl:attribute name="OC:{local-name()}" namespace="new-oc-namespace-uri">
 <xsl:value-of select="."/>
 </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

Thank you,
Pradnya




On Tue, Aug 16, 2011 at 11:15 AM, Pradnya Gawade
<pradnya(_dot_)gawade7(_at_)gmail(_dot_)com> wrote:
Hi David,

I think I am able to combine the 2 stylesheets with using the
namespaces exactly like in your stylesheet and tweaking my first
stylesheet a bit for prefixes I am using and putting name, priority
attributes appropriately for templates in your stylesheet.
Thanks again!

- Pradnya







On Tue, Aug 16, 2011 at 10:50 AM, Pradnya Gawade
<pradnya(_dot_)gawade7(_at_)gmail(_dot_)com> wrote:
Thanks a lot David.

One last thing I am thinking is if there is any way I can combine the
2 stylesheets I am using for the entire task. Meaning, before I apply
your stylehsheet I use the one which I have specified in my original
post and then output of the same I pass to your stylesheet to get the
final output. Ideally I would like to have everything in just one
stylesheet. I was thinking to make use "mode" attribute in the
templates to achieve this but looks like namespace declarations could
be a problem. Because in my 1st stylesheet e.g. default namespace is
my old default stylesheet and in yours it needs to be changed to the
new default stylesheet and similar namespace differences.

- Pradnya





On Tue, Aug 16, 2011 at 10:40 AM, David Carlisle 
<davidc(_at_)nag(_dot_)co(_dot_)uk> wrote:
On 16/08/2011 15:25, Pradnya Gawade wrote:

but it looks for the
elements corresponding the namespace uri in your stylesheet
corresponding to prefix 'olddef'; however the prefix in input xml for
the same namespace uri could be different.

Exactly, yes.

 All xpath matching uses the expanded qname (namespace URI + local name) 
The
prefixes used in the source and the stylesheet are essentially arbitrary 
and
(mostly) not used after the orignal parsing of the documents, where of
course they are needed to associate each element with its namespace.

David


--
google plus: https:/profiles.google.com/d.p.carlisle

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>