RE: [xsl] How to change namespace prefixes in a import file hierarchy? Or at least append som pattern?

Subject: RE: [xsl] How to change namespace prefixes in a import file hierarchy? Or at least append som pattern?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 21 Dec 2009 12:50:31 -0000
You need to do two things: (a) change the namespace nodes to use a different
prefix, and (b) change the QName-valued attributes to use a different
prefix.

For (a):

<xsl:template match="*">
  <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
    <xsl:for-each select="namespace::*">
       <xsl:namespace name="{if (local-name() = 'oldprefix') then
'newprefix' else local-name()}"
            select="string(.)"/>
       </
    </
    <xsl:apply-templates select="@*, node()"/>
  </
</ 

For (b)

<xsl:template match="attribute(*, xs:QName)">
  <xsl:attribute name="{name()}" select="concat(if (get-prefix-from-QName(.)
= 'oldprefix' then 'newprefix' else get-prefix-from-QName(.), ':',
get-local-name-from-QName(.))"/>

That's assuming a schema-aware transformation (using the schema-for-schemas)
so you can detect the QName-valued attributes by type. The alternative is to
match them all explicitly by name.

Not tested (and probably has bugs) but I hope it suggests a way forward.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


> -----Original Message-----
> From: Ben Stover [mailto:bxstover@xxxxxxxxxxx] 
> Sent: 21 December 2009 12:36
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] How to change namespace prefixes in a import 
> file hierarchy? Or at least append som pattern?
> 
> How to append a pattern to a namespace prefix? How to change 
> a namespace prefix in a import file hierarchy?
> 
> Assume there is a directory (tree) of XSD schema files which 
> start with ONE single "root" XSD schema file "root.xsd" and 
> which in turn imports other, related XSD schema files.
> 
> Regarding the used, embedded namespaces and their prefix it 
> looks like similar to:
> 
> root.xsd:
> 
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
>      xmlns:mypref1="http://www.somedomain.com/foo/bar/";
>      ...
>      xmlns:other22="http://www.somedomain.com/blah/";
>      ....>
>      
>      <xsd:import namespace="http://www.somedomain.com/foo/bar/"; 
>                  schemaLocation="myfoobar/subschema1.xsd"/>
>      ....
>      <xsd:import namespace="http://www.somedomain.com/blah/";
>                  schemaLocation="myblah/subschema22.xsd"/>
>                  
>      <xsd:element name="..."  type="mypref1:myelement111Type"/>
>      <xsd:element name="..."  type="other22:myelement222Type"/>
>      ...
>      
> 
> Keep in mind that this is a simplified sample. In real life I 
> could have a system of dozends of imports, hundreds of 
> namespace prefixes and thousands of elements. 
> 
> Now I want to change all prefixes "mypref1" to "newpre7". I 
> could this manually but due to the amount of occurencies and 
> the necessary changes this is not feasible. 
> Furthermore I dont want to use an text editor replace 
> function because this could be ambigious and not much simpler.
> 
> I would like to use a XLST script which would do the job for 
> me (after changing just a "from" and a "to" pattern inside 
> this XSLT script.
> 
> Is this possible somehow?
> 
> All the prefix occurencies should be changed in the prefix 
> definition and the usage places (e.g. <xsd:element>) in all 
> files of the import hierarchy.
> 
> How can I do this?
> 
> If a pure change is not possible: Can I at least append some 
> postfix to a prefix (e.g. add
> 
> from: mypref1
> to:   mypref1new
> 
> Thank you
> Ben

Current Thread