以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  XMI到XML Schema的XSLT转换例子,我遇到问题!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=12762)


--  作者:wxb_nudt
--  发布时间:12/14/2004 10:20:00 AM

--  XMI到XML Schema的XSLT转换例子,我遇到问题!
本例子来源于http://www-900.ibm.com/developerWorks/cn/xml/x-wxxm25/
我使用XMLSPY2004做XSLT转换时报错,先列举源文件如下,希望大家一起做出来:
XMI文件:ModelAddress1.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ModelAddress1.xsl"?>
<XMI xmi.version="1.2" xmlns:UML="org.omg/UML/1.4">
 <XMI.header>
  <XMI.documentation>
   <XMI.exporter>ananas.org stylesheet</XMI.exporter>
  </XMI.documentation>
  <XMI.metamodel xmi.name="UML" xmi.version="1.4"/>
 </XMI.header>
 <XMI.content>
  <UML:Model xmi.id="M.1" name="address" visibility="public" isSpecification="fase" isRoot="false" isLeaf="false" isAbstract="false">
   <UML:Namespace.ownedElement>
    <UML:Stereotype xmi.id="S.1" name="root" visibility="public" isSpecification="false" baseClass="Class">
            </UML:Stereotype>
    <UML:TagDefinition xmi.id="TD.1" name="position" isSpecification="false" tagType="String">
     <UML:TagDefinition.owner>
      <UML:Stereotype xmi.id="S.1"/>
     </UML:TagDefinition.owner>
    </UML:TagDefinition>
    <UML:Class xmi.id="C.1" name="person" visibility="public" isSpecification="false" namespace="M.1" isRoot="true" isLeaf="true" isAbstract="false" isActive="false">
     <UML:ModelElement.stereotype>
      <UML:Stereotype xmi.idref="S.1"/>
     </UML:ModelElement.stereotype>
     <UML:Classifier.feature>
      <UML:Attribute xmi.id="A.1" name="name" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.2" name="telephone" visibility="private" isSpecification="false" ownerScope="instance">
       <UML:StructuralFeature.multiplicity>
        <UML:Multiplicity>
         <UML:Multiplicity.range>
          <UML:MultiplicityRange lower="0" upper="5"/>
         </UML:Multiplicity.range>
        </UML:Multiplicity>
       </UML:StructuralFeature.multiplicity>
      </UML:Attribute>
      <UML:Attribute xmi.id="A.3" name="fax" visibility="private" isSpecification="false" ownerScope="instance">
       <UML:StructuralFeature.multiplicity>
        <UML:Multiplicity>
         <UML:Multiplicity.range>
          <UML:MultiplicityRange lower="0" upper="5"/>
         </UML:Multiplicity.range>
        </UML:Multiplicity>
       </UML:StructuralFeature.multiplicity>
      </UML:Attribute>
      <UML:Attribute xmi.id="A.4" name="email" visibility="private" isSpecification="false" ownerScope="instance">
       <UML:StructuralFeature.multiplicity>
        <UML:Multiplicity>
         <UML:Multiplicity.range>
          <UML:MultiplicityRange lower="0" upper="5"/>
         </UML:Multiplicity.range>
        </UML:Multiplicity>
       </UML:StructuralFeature.multiplicity>
      </UML:Attribute>
     </UML:Classifier.feature>
    </UML:Class>
    <UML:Class xmi.id="C.2" name="address" visibility="public" isSpecification="false" namespace="M.1" isRoot="true" isLeaf="true" isAbstract="false" isActive="false">
     <UML:Classifier.feature>
      <UML:Attribute xmi.id="A.5" name="name" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.6" name="street" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.7" name="zip" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.8" name="region" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.9" name="city" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.10" name="country" visibility="private" isSpecification="false" ownerScope="instance"/>
     </UML:Classifier.feature>
    </UML:Class>
    <UML:Class xmi.id="C.3" name="job" visibility="public" isSpecification="false" namespace="M.1" isRoot="true" isLeaf="true" isAbstract="false" isActive="false">
     <UML:Classifier.feature>
      <UML:Attribute xmi.id="A.11" name="title" visibility="private" isSpecification="false" ownerScope="instance"/>
      <UML:Attribute xmi.id="A.12" name="organization" visibility="private" isSpecification="false" ownerScope="instance"/>
     </UML:Classifier.feature>
    </UML:Class>
    <UML:Association xmi.id="A.1" isSpecification="false">
     <UML:ModelElement.taggedValue>
      <UML:TaggedValue xmi.id="T.1" isSpecification="false" dataValue="20">
       <UML:TaggedValue.type>
        <UML:TagDefinition xmi.idref="TD.1"/>
       </UML:TaggedValue.type>
      </UML:TaggedValue>
     </UML:ModelElement.taggedValue>
     <UML:Association.connection>
      <UML:AssociationEnd xmi.id="AE.1" visibility="public" isSpecification="false" isNavigable="true">
       <UML:AssociationEnd.participant>
        <UML:Class xmi.idref="C.1"/>
       </UML:AssociationEnd.participant>
      </UML:AssociationEnd>
      <UML:AssociationEnd xmi.id="AE.2" visibility="public" isSpecification="false" isNavigable="true">
       <UML:AssociationEnd.participant>
        <UML:Class xmi.idref="C.2"/>
       </UML:AssociationEnd.participant>
       <UML:AssociationEnd.multiplicity>
        <UML:Multiplicity>
         <UML:Multiplicity.range>
          <UML:MultiplicityRange lower="1" upper="5"/>
         </UML:Multiplicity.range>
        </UML:Multiplicity>
       </UML:AssociationEnd.multiplicity>
      </UML:AssociationEnd>
     </UML:Association.connection>
    </UML:Association>
    <UML:Association xmi.id="A.2" isSpecification="false">
     <UML:ModelElement.taggedValue>
      <UML:TaggedValue xmi.id="T.2" isSpecification="false" dataValue="120">
       <UML:TaggedValue.type>
        <UML:TagDefinition xmi.idref="TD.1"/>
       </UML:TaggedValue.type>
      </UML:TaggedValue>
     </UML:ModelElement.taggedValue>
     <UML:Association.connection>
      <UML:AssociationEnd xmi.id="AE.3" visibility="public" isSpecification="false" isNavigable="true">
       <UML:AssociationEnd.participant>
        <UML:Class xmi.idref="C.1"/>
       </UML:AssociationEnd.participant>
      </UML:AssociationEnd>
      <UML:AssociationEnd xmi.id="AE.4" visibility="public" isSpecification="false" isNavigable="true">
       <UML:AssociationEnd.participant>
        <UML:Class xmi.idref="C.3"/>
       </UML:AssociationEnd.participant>
      </UML:AssociationEnd>
     </UML:Association.connection>
    </UML:Association>
   </UML:Namespace.ownedElement>
  </UML:Model>
 </XMI.content>
</XMI>


XSL文件:ModelAddress1.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:xi="http://ananas.org/2002/xi/rules"
                xmlns:UML="org.omg/UML/1.4"
                exclude-result-prefixes="UML"
                version="1.0"
                xmlns:xalan="http://xml.apache.org/xslt">

<xsl:output xi:suffix="xsd" indent="yes" xalan:indent-amount="2"/>

<xsl:variable name="root-id" select="/XMI/XMI.content/UML:Model/UML:Namespace.ownedElement/UML:Stereotype[@name='root']/@xmi.id"/>
<xsl:variable name="position-id" select="/XMI/XMI.content/UML:Model/UML:Namespace.ownedElement/UML:TagDefinition[@name='position']/@xmi.id"/>

<xsl:template match="XMI[@xmi.version='1.2']">
   <xsl:apply-templates select="XMI.content/UML:Model"/>
</xsl:template>

<xsl:template match="XMI">
   <xsl:message terminate="yes">Unknown XMI version</xsl:message>
</xsl:template>

<xsl:template match="UML:Model">
   <xs:schema targetNamespace="http://psol.com/uml/{@name}">
      <xsl:apply-templates select="UML:Namespace.ownedElement/UML:Class[UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref=$root-id]"/>
   </xs:schema>
</xsl:template>

<xsl:template match="UML:Class">
   <xsl:param name="multiplicity"/>
   <xsl:variable name="id" select="@xmi.id"/>
   <xs:element name="{@name}">
      <!-- test because the processor reports an error if the parameter was not set
           (i.e. for root elements, the parameter is never set)                     -->
      <xsl:if test="$multiplicity">
         <xsl:apply-templates select="$multiplicity"/>
      </xsl:if>
      <xs:complexType>
         <xs:sequence>
            <xsl:apply-templates/>
            <xsl:apply-templates select="//UML:Association[UML:Association.connection/UML:AssociationEnd[1]/UML:AssociationEnd.participant/UML:Class[@xmi.idref=$id]]"
                                 mode="association">
               <xsl:sort select="UML:ModelElement.taggedValue/UML:TaggedValue[UML:TaggedValue.type/UML:TagDefinition[@xmi.idref=$position-id]]/@dataValue"
                         data-type="number"/>
            </xsl:apply-templates>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xsl:template>

<xsl:template match="UML:Association" mode="association">
   <xsl:variable name="id" select="UML:Association.connection/UML:AssociationEnd[2]/UML:AssociationEnd.participant/UML:Class/@xmi.idref"/>
   <xsl:apply-templates select="//UML:Class[@xmi.id=$id]" mode="association">
      <xsl:with-param name="multiplicity" select=".//UML:MultiplicityRange"/>
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="UML:Class[UML:ModelElement.stereotype/UML:Stereotype/@xmi.idref=$root-id]" mode="association">
   <xsl:param name="multiplicity"/>
   <xs:element ref="{@name}">
      <xsl:apply-templates select="$multiplicity"/>
   </xs:element>
</xsl:template>

<xsl:template match="UML:Class" mode="association">
   <xsl:param name="multiplicity"/>
   <xsl:apply-templates select=".">
      <xsl:with-param name="multiplicity" select="$multiplicity"/>
   </xsl:apply-templates>
</xsl:template>

<xsl:template match="UML:Attribute">
   <xs:element name="{@name}" type="xs:string">
      <xsl:apply-templates select=".//UML:MultiplicityRange"/>
   </xs:element>
</xsl:template>

<xsl:template match="UML:MultiplicityRange">
   <xsl:if test="@lower and number(@lower) != 1">
      <xsl:attribute name="minOccurs"><xsl:value-of select="@lower"/></xsl:attribute>
   </xsl:if>
   <xsl:choose>
      <xsl:when test="@upper and number(@upper) = -1">
         <xsl:attribute name="maxOccurs">unbounded</xsl:attribute>
      </xsl:when>
      <xsl:when test="@upper and number(@upper) != 1">
         <xsl:attribute name="maxOccurs"><xsl:value-of select="@upper"/></xsl:attribute>
      </xsl:when>
   </xsl:choose>
</xsl:template>

<xsl:template match="text()">
   <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

</xsl:stylesheet>

XMLSPY报错为:
此表达式中不能使用变量
UML:Class[UML:ModelElement.steroetype/UML:Steroetype/@xmi.idref=$root-id]
它说这个root-id不能在这儿使用??


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms