在 xslt 中更改名称空间 uri
Change namespace uri in xslt
我正在尝试转换由 Enterprise Architect 生成的 xmi 文件,以便它被 eclipse 工具接受。
我需要更改的一件事是前缀 uml: 来自“http://www.omg.org/spec/UML/20090901" to "http://www.eclipse.org/uml2/2.0.0/UML”
的名称空间 uri
输入:
<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1"
xmlns:uml="http://www.omg.org/spec/UML/20090901"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" >
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
<uml:Model xmi:type="uml:Model" name="EA_Model">
<!-- content of the model -->
</uml:Model>
</xmi:XMI>
预期输出:
<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" >
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
<uml:Model xmi:type="uml:Model" name="EA_Model">
<!-- content of the model -->
</uml:Model>
</xmi:XMI>
我尝试了什么:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:uml="http://www.omg.org/spec/UML/20090901"
exclude-result-prefixes="#all">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- update uml:namespace to "http://www.eclipse.org/uml2/2.0.0/UML" -->
<xsl:template match="uml:*">
<xsl:element name="{local-name()}" namespace="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:apply-templates select="@*, node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是从节点中删除了 uml: 前缀并为其添加了默认名称空间。
绝对清楚,我只需要字符串
xmlns:uml="http://www.omg.org/spec/UML/20090901"
将替换为
xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
将name="{local-name()}"
替换为name="uml:{local-name()}"
如果您不指定前缀,系统将不会使用前缀。
这会产生预期的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="#all" version="3.0"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="windows-1252" indent="yes" method="xml" version="1.0"/>
<xsl:template match="uml:*" xmlns:uml="http://www.omg.org/spec/UML/20090901">
<xsl:element name="{name()}" namespace="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xmi:XMI">
<xmi:XMI xmi:version="2.1"
xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
<xsl:apply-templates select="@* | node()"/>
</xmi:XMI>
</xsl:template>
</xsl:stylesheet>
我确信有更简单的解决方案。但这可能是一个开始。
扩展答案(请参阅评论中的问题):用此替换第三个模板以复制除 xmlns:uml
.
之外的所有命名空间声明
<xsl:template match="xmi:XMI">
<xmi:XMI xmi:version="2.1" xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:copy-of select="namespace-node()[not(. = 'http://www.omg.org/spec/UML/20090901')]"/>
<xsl:apply-templates select="@* | node()"/>
</xmi:XMI>
</xsl:template>
我正在尝试转换由 Enterprise Architect 生成的 xmi 文件,以便它被 eclipse 工具接受。
我需要更改的一件事是前缀 uml: 来自“http://www.omg.org/spec/UML/20090901" to "http://www.eclipse.org/uml2/2.0.0/UML”
的名称空间 uri输入:
<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1"
xmlns:uml="http://www.omg.org/spec/UML/20090901"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" >
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
<uml:Model xmi:type="uml:Model" name="EA_Model">
<!-- content of the model -->
</uml:Model>
</xmi:XMI>
预期输出:
<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" >
<xmi:Documentation exporter="Enterprise Architect" exporterVersion="6.5"/>
<uml:Model xmi:type="uml:Model" name="EA_Model">
<!-- content of the model -->
</uml:Model>
</xmi:XMI>
我尝试了什么:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:uml="http://www.omg.org/spec/UML/20090901"
exclude-result-prefixes="#all">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- update uml:namespace to "http://www.eclipse.org/uml2/2.0.0/UML" -->
<xsl:template match="uml:*">
<xsl:element name="{local-name()}" namespace="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:apply-templates select="@*, node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是从节点中删除了 uml: 前缀并为其添加了默认名称空间。
绝对清楚,我只需要字符串
xmlns:uml="http://www.omg.org/spec/UML/20090901"
将替换为
xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
将name="{local-name()}"
替换为name="uml:{local-name()}"
如果您不指定前缀,系统将不会使用前缀。
这会产生预期的结果:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="#all" version="3.0"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="windows-1252" indent="yes" method="xml" version="1.0"/>
<xsl:template match="uml:*" xmlns:uml="http://www.omg.org/spec/UML/20090901">
<xsl:element name="{name()}" namespace="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xmi:XMI">
<xmi:XMI xmi:version="2.1"
xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
<xsl:apply-templates select="@* | node()"/>
</xmi:XMI>
</xsl:template>
</xsl:stylesheet>
我确信有更简单的解决方案。但这可能是一个开始。
扩展答案(请参阅评论中的问题):用此替换第三个模板以复制除 xmlns:uml
.
<xsl:template match="xmi:XMI">
<xmi:XMI xmi:version="2.1" xmlns:uml="http://www.eclipse.org/uml2/2.0.0/UML">
<xsl:copy-of select="namespace-node()[not(. = 'http://www.omg.org/spec/UML/20090901')]"/>
<xsl:apply-templates select="@* | node()"/>
</xmi:XMI>
</xsl:template>