Subject: RE: How to change namespace prefixes in a import file hierarchy? Or at least append som pattern?
From: "Ben Stover" <bxstover@xxxxxxxxxxx>
Date: Tue, 05 Jan 2010 17:04:18 +0100
|
Hello Mike,
thank you for your sample code but the the closing tags seem not to fit together. I completed your code as follows and removed one "</"
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"></xsl:output>
<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:for-each>
</xsl:element>
<xsl:apply-templates select="@*, node()"/>
</xsl:template>
<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(.))"/>
</xsl:template>
</xsl:stylesheet>
But got the following compiling errors:
Saxon 9.1.0.7J from Saxonica
Java version 1.5.0_06
Error at xsl:template on line 18 column 46 of renameNamespacesKay.xsl:
XPST0081: XSLT Pattern syntax error at char 13 on line 18 in {attribute(*, xs:QName)}:
Undeclared namespace prefix {xs}
Error at xsl:attribute on line 19 column 177 of renameNamespacesKay.xsl:
XPST0017: XPath syntax error at char 36 on line 19 in {...ix-from-QName(.) = 'oldpref...}:
Unknown system function get-prefix-from-QName()
Failed to compile stylesheet. 2 errors detected.
XSLT run finished
How can I complete the code successfully?
Thank you
Ben
On Mon, 21 Dec 2009 12:50:31 -0000, Michael Kay wrote:
>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: 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
|