2015-08-28 141 views
0

我試圖在node中解析xmi:type。但不能成功地做到這一點。我附加了輸入文件(uml-xmi格式),我的xslt輸入和我得到的輸出(xml格式)。每當我看到「xmi:type = uml:OpaqueAction」時,我想修改輸出以寫入一行。我能夠訪問同一行中的「名稱」,但不能訪問xmi:type。不知道爲什麼使用XSL將XMI轉換爲XML

UML,XMI輸入

<?xml version="1.0" encoding="UTF-8"?> 
<uml:Model xmi:version="20110701" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:uml="http://www.eclipse.org/uml2/4.0.0/UML" xmi:id="_OlYJkC9-EeWyX7UKkcyxiw" name="model"> 
    <packagedElement xmi:type="uml:Activity" xmi:id="_OlYJkS9-EeWyX7UKkcyxiw" name="Activity1" node="_XjLyEC9-EeWyX7UKkcyxiw _ZfIhYC9-EeWyX7UKkcyxiw _cK4V8C9-EeWyX7UKkcyxiw _fE2zwC9-EeWyX7UKkcyxiw _F67sgC9_EeWyX7UKkcyxiw"> 
     <edge xmi:type="uml:ControlFlow" xmi:id="_jzMLIC9-EeWyX7UKkcyxiw" name="ControlFlow" source="_XjLyEC9-EeWyX7UKkcyxiw" target="_ZfIhYC9-EeWyX7UKkcyxiw"/> 
     <edge xmi:type="uml:ControlFlow" xmi:id="_lieXcC9-EeWyX7UKkcyxiw" name="ControlFlow1" source="_ZfIhYC9-EeWyX7UKkcyxiw" target="_cK4V8C9-EeWyX7UKkcyxiw"/> 
     <edge xmi:type="uml:ControlFlow" xmi:id="_m0Vn8C9-EeWyX7UKkcyxiw" name="Timer expired" source="_cK4V8C9-EeWyX7UKkcyxiw" target="_fE2zwC9-EeWyX7UKkcyxiw"/> 
     <edge xmi:type="uml:ControlFlow" xmi:id="_wrhvgC9-EeWyX7UKkcyxiw" name="Timer not expired" source="_cK4V8C9-EeWyX7UKkcyxiw" target="_cK4V8C9-EeWyX7UKkcyxiw"/> 
     <edge xmi:type="uml:ControlFlow" xmi:id="_Hcj3UC9_EeWyX7UKkcyxiw" name="ControlFlow2" source="_fE2zwC9-EeWyX7UKkcyxiw" target="_F67sgC9_EeWyX7UKkcyxiw"/> 
     <node xmi:type="uml:InitialNode" xmi:id="_XjLyEC9-EeWyX7UKkcyxiw" name="Start" outgoing="_jzMLIC9-EeWyX7UKkcyxiw"/> 
     <node xmi:type="uml:OpaqueAction" xmi:id="_ZfIhYC9-EeWyX7UKkcyxiw" name="Load and Enable Timer" visibility="package" outgoing="_lieXcC9-EeWyX7UKkcyxiw" incoming="_jzMLIC9-EeWyX7UKkcyxiw"/> 
     <node xmi:type="uml:DecisionNode" xmi:id="_cK4V8C9-EeWyX7UKkcyxiw" name="Has timer expired?" outgoing="_m0Vn8C9-EeWyX7UKkcyxiw _wrhvgC9-EeWyX7UKkcyxiw" incoming="_lieXcC9-EeWyX7UKkcyxiw _wrhvgC9-EeWyX7UKkcyxiw"/> 
     <node xmi:type="uml:OpaqueAction" xmi:id="_fE2zwC9-EeWyX7UKkcyxiw" name="Service the Timer ISR" outgoing="_Hcj3UC9_EeWyX7UKkcyxiw" incoming="_m0Vn8C9-EeWyX7UKkcyxiw"> 
      <inputValue xmi:type="uml:ActionInputPin" xmi:id="_82lIMDRBEeWdiarL2UAMaQ" name="interrupt"> 
       <upperBound xmi:type="uml:LiteralInteger" xmi:id="_82lIMTRBEeWdiarL2UAMaQ" value="1"/> 
      </inputValue> 
     </node> 
     <node xmi:type="uml:ActivityFinalNode" xmi:id="_F67sgC9_EeWyX7UKkcyxiw" name="ActivityFinalNode" incoming="_Hcj3UC9_EeWyX7UKkcyxiw"/> 
    </packagedElement> 
</uml:Model> 

XSLT INPUT

<xsl:stylesheet version="1.0" xmlns:UML="http://www.eclipse.org/uml2/4.0.0/UML" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns='uri:sadf' exclude-result-prefixes="UML"> 
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 
<xsl:template match="/UML:Model"> 
    <?xml version="1.0"?> 
    <sdf3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="fsmsadf" xsi:noNamespaceSchemaLocation="http://www.es.ele.tue.nl/sdf3/xsd/sdf3-fsmsadf.xsd"> 
    <applicationGraph name="g"> 
     <xsl:apply-templates /> 
    </applicationGraph> 
</sdf3> 
</xsl:template> 
<xsl:template match="packagedElement"> 
    <fsmsadf> 
    <scenariograph name="sg0" type="t"> 
     <xsl:apply-templates select="node" /> 
    </scenariograph> 
    </fsmsadf> 
</xsl:template> 
<xsl:template match="node"> 
    <xsl:if test="@'xmi:type'='uml:OpaqueAction'"> 
     <actor name="{@name}" source="{@outgoing}" /> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

輸出(.XML)

<?xml version="1.0" encoding="UTF-8"?> 
<sdf3 xmlns="uri:sadf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.es.ele.tue.nl/sdf3/xsd/sdf3-fsmsadf.xsd" type="fsmsadf" version="1.0"> 
<applicationGraph name="g"> 
<fsmsadf> 
    <scenariograph type="t" name="sg0" /> 
#Need these lines  <actor name="Load and Enable Timer" source="whatever_input is here"> 
#Need these lines  <actor name="Service the timer ISR" source="whatever_input is here"> 
</fsmsadf> 
</applicationGraph> 
</sdf3> 
+0

什麼是您預期的輸出? –

回答

0

沒有與此行的一個問題:

<xsl:if test="@'xmi:type'='uml:OpaqueAction'"> 

的正確語法應該是這樣的:

<xsl:if test="@xmi:type='uml:OpaqueAction'"> 

你不需要的屬性名左右撇號。你只需要它們圍繞一個字符串。您還需要在XSLT中聲明xmi命名空間,命名空間URI與XML中的內容匹配。如果您嘗試運行您的XSLT,則應在錯誤消息中突出顯示。

事實上,就這樣過另一種錯誤,因爲這些線路是無效的

<xsl:template match="/UML:Model"> 
    <?xml version="1.0"?> 

或許這只是一個錯字與你的問題,本來應該在XSLT的開始,但如果您實際上正在嘗試在輸出XML中包含xml聲明,您只需在xsl:output元素上將omit-xml-declaration更改爲「no」即可。

試試這個XSLT

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns='uri:sadf' 
xmlns:UML="http://www.eclipse.org/uml2/4.0.0/UML" 
xmlns:xmi="http://www.omg.org/spec/XMI/20110701" 
exclude-result-prefixes="UML xmi"> 
<xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" /> 

<xsl:template match="/UML:Model"> 
    <sdf3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="fsmsadf" xsi:noNamespaceSchemaLocation="http://www.es.ele.tue.nl/sdf3/xsd/sdf3-fsmsadf.xsd"> 
    <applicationGraph name="g"> 
     <xsl:apply-templates /> 
    </applicationGraph> 
</sdf3> 
</xsl:template> 

<xsl:template match="packagedElement"> 
    <fsmsadf> 
    <scenariograph name="sg0" type="t"> 
     <xsl:apply-templates select="node" /> 
    </scenariograph> 
    </fsmsadf> 
</xsl:template> 

<xsl:template match="node"> 
    <xsl:if test="@xmi:type='uml:OpaqueAction'"> 
     <actor name="{@name}" source="{@outgoing}" /> 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet>