XSLT - 不得复制节点值 [英] XSLT - node values shall not be copied

查看:37
本文介绍了XSLT - 不得复制节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XSLT 转换一些 xmi 文件.一切正常,但我不明白为什么我的模板packagedElement"复制了 标签值Version 1.0"和EAUML Version:1.0"(见输出).>

xmi 文件:

<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:thecustomprofile="http://www.sparxsystems.com/profiles/thecustomprofile/1.0" xmlns:EAUML="http://www.sparxsystems.com/profiles/EAUML/1.0"><uml:Model xmi:type="uml:Model" name="EA_Model"visibility="public"><packagedElement xmi:type="uml:Package" name="Test"visibility="public">...</packagedElement></uml:模型><xmi:扩展><个人资料><uml:Profile xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1/" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:id="thecustomprofile" nsPrefix="thecustomprofile" name="thecustomprofile" metamodelReference="mmref01"><ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="thecustomprofile"><身体>版本:1.0</ownedComment><packagedElement xmi:type="uml:Stereotype" xmi:id="enum" name="enum"/>...</uml:个人资料><uml:Profile xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1/" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:id="8C9E6706-8" nsPrefix="EAUML" name="EAUML" metamodelReference="mmref01"><ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="8C9E6706-8"><body>EAUML 版本:1.0</body></ownedComment>...</uml:个人资料></个人资料></xmi:扩展></xmi:XMI>

XSLT 文件( 中的代码与我的问题无关):

<xsl:strip-space elements="*"/><xsl:output indent="yes"/><xsl:template match="/"><ecore:EPackage><xsl:apply-templates mode="packagedElement"/></ecore:EPackage></xsl:模板><xsl:template match="packagedElement" mode="packagedElement"><xsl:if test="(@xmi:type='uml:Package')"></xsl:if></xsl:模板></xsl:stylesheet>

输出:

<ecore:EPackage xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uml="http://www.omg.org/spec/UML/20131001">版本:1.0 EAUML 版本:1.0 </ecore:EPackage>

解决方案

你看到的是 内置模板规则 应用于未与您的任何模板显式匹配的节点.

要解决此问题,请添加一个模板以防止默认情况下复制文本节点:

或者 - 最好,恕我直言 - 更有选择性地应用模板.

I want to transform some xmi files using XSLT. Everything works fine, but I do not understand why the <body> tag values "Version 1.0" and "EAUML Version:1.0" are copied by my template "packagedElement" (see output).

xmi File:

<?xml version="1.0" encoding="windows-1252"?>
<xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:thecustomprofile="http://www.sparxsystems.com/profiles/thecustomprofile/1.0" xmlns:EAUML="http://www.sparxsystems.com/profiles/EAUML/1.0">
    <uml:Model xmi:type="uml:Model" name="EA_Model" visibility="public">
        <packagedElement xmi:type="uml:Package" name="Test" visibility="public">
            ...
        </packagedElement>
    </uml:Model>
    <xmi:Extension>
        <profiles>
            <uml:Profile xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1/" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:id="thecustomprofile" nsPrefix="thecustomprofile" name="thecustomprofile" metamodelReference="mmref01">
                <ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="thecustomprofile">
                    <body> Version:1.0</body>
                </ownedComment>
                <packagedElement xmi:type="uml:Stereotype" xmi:id="enum" name="enum"/>
                ...
            </uml:Profile>
            <uml:Profile xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1/" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmi:id="8C9E6706-8" nsPrefix="EAUML" name="EAUML" metamodelReference="mmref01">
                <ownedComment xmi:type="uml:Comment" xmi:id="comment01" annotatedElement="8C9E6706-8">
                    <body>EAUML Version:1.0</body>
                </ownedComment>
                ...
            </uml:Profile>
        </profiles>
    </xmi:Extension>
</xmi:XMI>

XSLT file (code inside <xsl:if> is irrelevant for my problem):

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:uml="http://www.omg.org/spec/UML/20131001"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:template match="/">
    <ecore:EPackage>
        <xsl:apply-templates mode="packagedElement" />
    </ecore:EPackage>
</xsl:template>

<xsl:template match="packagedElement" mode="packagedElement">
    <xsl:if test="(@xmi:type='uml:Package')">

    </xsl:if>
</xsl:template>

</xsl:stylesheet>

output:

<?xml version="1.0"?>
<ecore:EPackage xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:uml="http://www.omg.org/spec/UML/20131001"> Version:1.0  EAUML Version:1.0 </ecore:EPackage>

解决方案

What you see is the result of the built-in template rules being applied to nodes that aren't matched explicitly by any one of your templates.

To solve this, either add a template preventing text nodes being copied by default:

<xsl:template match="text()" mode="packagedElement"/>

or - preferably, IMHO - apply templates more selectively.

这篇关于XSLT - 不得复制节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆