2017-07-01 4 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の別の表現が見つかりました。 –

関連する問題