在XSLT中为每个副本包含标记

时间:2016-09-21 11:31:00

标签: xslt

下面是输入xml: -

<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应用于每个数据副本

2 个答案:

答案 0 :(得分:0)

我无法看到xsd,因此我认为标记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>

重要:

  • 名称为dateAmtGrossAmtNetContribType的所有4个元素必须按特定顺序排列!
  • 必须存在所有4个要素;如果缺少一个,xslt将失败并输出错误的数据[可能输出有效,但内容错误]

答案 1 :(得分: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>

这假设输入将始终以4个为一组列出key元素,其中<key name="Date">是其组中的第一个。