2016-09-21 7 views
1

は、入力XMLでコピーします -はすべてのためにXSLTのタグを含める以下

<request version="1" type="PrintFPDPackInput"> 

    <keys> 
<key name="Date" value="02/01/2010 01:00:25" /> 
<key name="AmtGross" value="22.33" /> 
<key name="AmtNet" value="17.86" /> 
<key name="ContribType" value="Individual" /> 

<key name="Date" value="01/01/2010 01:00:26" /> 
<key name="AmtGross" value="22.25" /> 
<key name="AmtNet" value="17.80" /> 
<key name="ContribType" value="Individual" /> 

<key name="Date" value="12/01/2009 01:00:27" /> 
<key name="AmtGross" value="22.25" /> 
<key name="AmtNet" value="17.80" /> 
<key name="ContribType" value="Individual" /> 


    </keys> 
</request> 

処理さXSLT: -

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <!-- <xsl:param name="User"/> 
    <xsl:param name="Password"/> --> 

    <xsl:template match="/"> 

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
         xmlns:fpd="http://zip.uk.zurich.com/fpdservice"> 
     <soapenv:Header/> 

<soapenv:Body> 
<fpd:CheckFPD> 

<xsl:copy> 
<policy> 
     <xsl:apply-templates select="request/keys/key[@name = 'Date' or @name = 'AmtGross' or @name = 'AmtNet' or @name = 'ContribType']" /> 
</policy> 
    </xsl:copy> 






</fpd:CheckFPD> 
      </soapenv:Body> 
     </soapenv:Envelope> 
    </xsl:template> 

<xsl:template match="key[@name = 'Date' or @name = 'AmtGross' or @name = 'AmtNet' or @name = 'ContribType' ]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="@value" /> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

これは私が以下のように取得していますエラー出力です:http://xslttest.appspot.com/

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:fpd="http://zip.uk.zurich.com/fpdservice"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <fpd:CheckFPD> 
     <policy> 
      <Date>02/01/2010 01:00:25</Date> 
      <AmtGross>22.33</AmtGross> 
      <AmtNet>17.86</AmtNet> 
      <ContribType>Individual</ContribType> 
      <Date>01/01/2010 01:00:26</Date> 
      <AmtGross>22.25</AmtGross> 
      <AmtNet>17.80</AmtNet> 
      <ContribType>Individual</ContribType> 
      <Date>12/01/2009 01:00:27</Date> 
      <AmtGross>22.25</AmtGross> 
      <AmtNet>17.80</AmtNet> 
      <ContribType>Individual</ContribType> 
     </policy> 
     </fpd:CheckFPD> 
    </soapenv:Body> 
</soapenv:Envelope> 

次の形式で出力が期待されます。 -

<ListOfPolicyReceipts> 
          <PolicyReceipts> 
           <Date>02/01/2010 01:00:25</Date> 
           <AmtGross>22.33</AmtGross> 
           <AmtNet>17.86</AmtNet> 
           <ContribType>Individual</ContribType> 
          </PolicyReceipts> 
          <PolicyReceipts> 
           <Date>01/01/2010 01:00:26</Date> 
           <AmtGross>22.25</AmtGross> 
           <AmtNet>17.80</AmtNet> 
           <ContribType>Individual</ContribType> 
          </PolicyReceipts> 
          <PolicyReceipts> 
           <Date>12/01/2009 01:00:27</Date> 
           <AmtGross>22.25</AmtGross> 
           <AmtNet>17.80</AmtNet> 
           <ContribType>Individual</ContribType> 
          </PolicyReceipts> 

が親切タグPolicyReceiptsが、私はXSDを見ることができないので、私が想定したデータ

+0

これが期待される出力であれば、スタイルシートはSOAPエンベロープとヘッダーを作成するのはなぜですか? –

+0

関連[スレッド](http://stackoverflow.com/questions/39575072/an-attribute-node-cannot-be-created-after-a-child-of-the-containing-element)。これは - 私はそれを「アドオン」 - スレッドと呼んでいます。 – uL1

答えて

0

のすべてのコピーに適用されるプロセスを示唆して、タグListOfPolicyReceiptsは、タグpolicyに置き換えられます。しかし、私はあなたのスタイルシートの正しい場所に次のコードを実装することができます。

は、おそらくあなたは、このいずれかを使用できます。

<ListOfPolicyReceipts> 
    <xsl:for-each select="request/keys/key[@name = 'Date']"> 
     <PolicyReceipts> 
      <xsl:apply-templates select=". | following-sibling::key[@name = 'AmtGross'][1] | following-sibling::key[@name = 'AmtNet'][1] | following-sibling::key[@name = 'ContribType'][1]"/> 
     </PolicyReceipts> 
    </xsl:for-each> 
</ListOfPolicyReceipts> 

重要:dateAmtGrossAmtNet

  • すべての4つの要素とContribTypeは、その特定の順序でなければなりません!
  • すべての4つの要素が存在しなければならない。 1が欠落している場合、XSLTは失敗し、出力誤ったデータが[おそらく出力は有効ですが、内容は間違っている]
+0

ありがとう!これは絶対にうまく動作しており、ステップも期待通りです。 – Raghava

0

掲載期待出力がで非常に簡単に達成することができます。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/request"> 
    <ListOfPolicyReceipts> 
     <xsl:apply-templates select="keys/key[@name = 'Date']"/> 
    </ListOfPolicyReceipts> 
</xsl:template> 

<xsl:template match="key"> 
    <PolicyReceipts> 
     <xsl:for-each select=". | following-sibling::key[position() &lt; 4]"> 
      <xsl:element name="{@name}"> 
       <xsl:value-of select="@value" /> 
      </xsl:element> 
     </xsl:for-each> 
    </PolicyReceipts> 
</xsl:template> 

</xsl:stylesheet> 

これは<key name="Date">がそのグループの最初であると、入力は常に4のグループでkey要素をリストすると仮定されます。

関連する問題