xslt基于字段为每个创建

时间:2017-05-27 10:54:20

标签: xml xslt

我有以下要求。输入文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<MyResponse>
   <resultStructure>
      <toProcess>true</toProcess>
      <records>
         <Id>ABCD</Id>
         <Name>Walmart|Reebok</Name>
         <City>Kuala Lumpur</City>
     <ResponseType>Radio Button</ResponseType>
      </records>
      <records>
         <Id>1234</Id>
         <Name>Tesco|ASDA|Amazon</Name>
         <City>London</City>
         <Country>United Kingdom</Country>
      <ResponseType>Picklist</ResponseType>
      </records>
      <records>
         <Id>ABCD</Id>
         <Name>ASDA</Name>
         <City>Birmingham</City>
         <Country>United Kingdom</Country>
    <ResponseType>Text</ResponseType>
      </records>
   </resultStructure>
</MyResponse>

当字段ResponseType的值为Picklist或Radio Button时,字段Name的值应溢出为一个dataProcessed类型,用于由pipe分隔的每个值: 期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <records>
      <dataProcessed>
         <FieldName>Id</FieldName>
         <FieldValue>ABCD</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>1</FieldName>
         <FieldValue>Walmart</FieldValue>
      </dataProcessed>
    <dataProcessed>
         <FieldName>2</FieldName>
         <FieldValue>Reebok</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>City</FieldName>
         <FieldValue>Kuala Lumpur</FieldValue>
      </dataProcessed>
   </records>
   <records>
      <dataProcessed>
         <FieldName>Id</FieldName>
         <FieldValue>1234</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>1</FieldName>
         <FieldValue>Tesco</FieldValue>
      </dataProcessed>
    <dataProcessed>
         <FieldName>2</FieldName>
         <FieldValue>ASDA</FieldValue>
      </dataProcessed>
    <dataProcessed>
         <FieldName>3</FieldName>
         <FieldValue>Amazon</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>City</FieldName>
         <FieldValue>London</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>Country</FieldName>
         <FieldValue>United Kingdom</FieldValue>
      </dataProcessed>
   </records>
   <records>
      <dataProcessed>
         <FieldName>Id</FieldName>
         <FieldValue>XYZ</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>Name</FieldName>
         <FieldValue>ASDA</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>City</FieldName>
         <FieldValue>London</FieldValue>
      </dataProcessed>
      <dataProcessed>
         <FieldName>Country</FieldName>
         <FieldValue>United Kingdom</FieldValue>
      </dataProcessed>
   </records>
</result>

以下是我正在尝试的代码:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/MyResponse">

<result>
<xsl:if test="resultStructure/toProcess = 'true'">

          <xsl:for-each select="resultStructure/records">
            <records>
                  <xsl:for-each select="./*">


<dataProcessed>


                 <name>
             <xsl:value-of select ="name(.)"/>
             </name>
                <valstr>
                <xsl:value-of select="."></xsl:value-of>
                </valstr>


 </dataProcessed>


                </xsl:for-each>
                </records>
               </xsl:for-each>

 </xsl:if> 
</result>


   </xsl:template>

</xsl:stylesheet>

如果ResponseType是&#39;单选按钮&#39;我无法使用条件将元素名称分成不同的部分。或者&#39;选择列表&#39;。如果我添加测试条件,我应该如何修改代码。请指导我。

0 个答案:

没有答案