2017-07-01 26 views
0

我試圖將使用通用模型發送到後端的XML表單轉換爲系統使用XSLT使用的內部XML模型。從通用模型到特定模型的XSLT轉換

通用模型由部分,行和表組成(表格基本上是一個數組/元素組)。 PK是來自tableRow的序列,而maxPK是一個計數。 MaxPK我可以在之後填充,不知道是否可以通過XSLT進行。

任何幫助,將不勝感激!

通用型號

<form> 
    <section> 
     <name>identification</name> 
     <sequence>1</sequence> 
     <line> 
      <sequence>0</sequence> 
      <field> 
       <name>firstName</name> 
       <value>JOHN</value> 
      </field> 
     </line> 
     <line> 
      <sequence>1</sequence> 
      <field> 
       <name>lastName</name> 
       <value>DOE</value> 
      </field> 
     </line> 
    </section> 
    <section> 
     <name>contactDetails</name> 
     <sequence>1</sequence> 
     <line> 
      <sequence>0</sequence> 
      <field> 
       <name>primaryPhone</name> 
       <value>+44 100 1234</value> 
      </field> 
     </line> 
     <table> 
      <name>secondaryPhoneGroup</name> 
      <tableRow> 
       <sequence>1</sequence> 
       <field> 
        <sequence>0</sequence> 
        <name>secondaryPhone</name> 
        <value>+44 100 1235</value> 
       </field> 
      </tableRow> 
      <tableRow> 
       <sequence>2</sequence> 
       <field> 
        <sequence>0</sequence> 
        <name>secondaryPhone</name> 
        <value>+44 100 1236</value> 
       </field> 
      </tableRow> 
     </table> 
    </section> 
</form> 

內模

<form> 
    <identification> 
     <firstName> 
      <asCurrent>JOHN</asCurrent> 
     </firstName> 
     <lastName> 
      <asCurrent>DOE</asCurrent> 
     </lastName> 
    </identification> 
    <contactDetails> 
     <primaryPhone> 
      <asCurrent>+44 100 1234</asCurrent> 
     </primaryPhone> 
     <secondaryPhoneGroup> 
      <secondaryPhone> 
       <pk>1</pk> 
       <phone> 
        <asCurrent>+44 100 1235</asCurrent> 
       </phone> 
      </secondaryPhone> 
      <secondaryPhone> 
       <pk>2</pk> 
       <phone> 
        <asCurrent>+44 100 1236</asCurrent> 
       </phone> 
      </secondaryPhone> 
      <maxPK>2</maxPK> 
     </secondaryPhoneGroup> 
    </contactDetails> 
</form> 
+1

那麼,什麼是規則,你有什麼嘗試?數據的位置在哪裏來自「pk」和「maxPK」,分別是元素個數的位置? –

+0

請將解決方案作爲答案發布,而不是更新您的問題。這是爲了避免混淆並幫助未來的訪問者。您可以在[修訂](https://stackoverflow.com/posts/44864560/revisions)中找到已刪除的解決方案。謝謝。 – Bugs

回答

1

大意如下嘗試:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

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

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="line"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="sequence"/> 

    <xsl:template match="section | field"> 
     <xsl:element name="{name}"> 
      <xsl:apply-templates select="* except name"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="*[name]/value"> 
     <asCurrent> 
      <xsl:apply-templates/> 
     </asCurrent> 
    </xsl:template> 

    <xsl:template match="table"> 
     <xsl:element name="{name}"> 
      <xsl:apply-templates select="* except name"/> 
      <maxPK> 
       <xsl:value-of select="count(tableRow)"/> 
      </maxPK> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="tableRow"> 
     <xsl:element name="{field/name}"> 
      <xsl:apply-templates select="* except name"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="tableRow/sequence"> 
     <pk> 
      <xsl:apply-templates/> 
     </pk> 
    </xsl:template> 

    <xsl:template match="tableRow/field[name = 'secondaryPhone']"> 
     <phone> 
      <xsl:apply-templates select="value"/> 
     </phone> 
    </xsl:template> 

</xsl:transform> 
+0

謝謝你的幫助! 我在原來的規範中犯了一個錯誤,使得它更加混亂,但在您的幫助下,我能夠破解它! *除了名稱符號不適用於我,但用* [not(self :: name)]取代它的確有竅門。 – Neil

+0

'except'是XSLT/XPath 2.0及更高版本的一部分,因爲您使用該版本標記了您的問題,因此我認爲它可以安全地使用。但是您已經找到了XSLT 1.0的替代表達式。 –