2017-07-01 18 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的替代表达式。 –