如何在多个元素中添加<!-[CDATA]]->

时间:2018-08-13 00:20:04

标签: xml xslt

我一直在寻找答案,但无法找到适合我的应用程序的答案。我找到的大多数答案是删除CDATA。
就我而言,我需要为所有GroupNum和RefNum元素添加CDATA。 任何帮助是极大的赞赏。

输入XML如下:

            <?xml version='1.0'?>
    <Table>
        <Group>
            <Contract>85585585556546</Contract>
            <Lname>ROBERT</Lname>
        </Group>
        <Source>
            <Id>175503459965</Id>
            <RefNum>201810855</RefNum>
            <GroupNum>
                <GroupNum>1100000020647052</GroupNum>
                <GroupNum>1010000020647053</GroupNum>
                <GroupNum>1020000020647065</GroupNum>
                <GroupNum>1200000020647199</GroupNum>
                <GroupNum>1060000020647202</GroupNum>
                <GroupNum>1400000020647217</GroupNum>
                <GroupNum>1080000020647292</GroupNum>
                <GroupNum>1600000020647359</GroupNum>
                <GroupNum>1900000020647360</GroupNum>
                <GroupNum>1050000020647374</GroupNum>
                <GroupNum>1400000020647425</GroupNum>
                <GroupNum>1030000020647427</GroupNum>
                <GroupNum>1600000020647477</GroupNum>
                <GroupNum>1090000020647484</GroupNum>
                <GroupNum>1020000020647485</GroupNum>
                <GroupNum>1060000020647487</GroupNum>
            </GroupNum>
        </Source>
    </Table>

这是所需的输出:

    <?xml version="1.0" encoding="utf-8"?>
    <Table>
         <TransactionId>175503459965</TransactionId>
         <RequestDateTime>2018-08-08</RequestDateTime>
         <UserId>12345</UserId>
        <Group>
          <RefNum>201810855</RefNum>
          <Id>175503459965</Id>               
          <GroupNum>
        <GroupNum><![CDATA[1100000020647052]]></GroupNum>
        <GroupNum><![CDATA[1010000020647053]]></GroupNum>
        <GroupNum><![CDATA[1020000020647065]]></GroupNum>
        <GroupNum><![CDATA[1200000020647199]]></GroupNum>
        <GroupNum><![CDATA[1060000020647202]]></GroupNum>
        <GroupNum><![CDATA[1400000020647217]]></GroupNum>
        <GroupNum><![CDATA[1080000020647292]]></GroupNum>
        <GroupNum><![CDATA[1600000020647359]]></GroupNum>
        <GroupNum><![CDATA[1900000020647360]]></GroupNum>
        <GroupNum><![CDATA[1050000020647374]]></GroupNum>
        <GroupNum><![CDATA[1400000020647425]]></GroupNum>
        <GroupNum><![CDATA[1030000020647427]]></GroupNum>
        <GroupNum><![CDATA[1600000020647477]]></GroupNum>
        <GroupNum><![CDATA[1090000020647484]]></GroupNum>
        <GroupNum><![CDATA[1020000020647485]]></GroupNum>
        <GroupNum><![CDATA[1060000020647487]]></GroupNum>
          </GroupNum>
                </Group>
    </Table>

到目前为止,这是我的XSLT:

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

      <xsl:output method="xml" indent="yes"/>
      <msxsl:text disable-output-escaping="yes"></msxsl:text>


      <xsl:template match="Table">       
          <xsl:element name="Table">
           <xsl:element name ="TransactionId">
            <xsl:value-of select="Group/Id"/>
           </xsl:element>               

           <xsl:element name="RequestDateTime">
            <xsl:value-of select="Date"/>
           </xsl:element>        

           <xsl:element name ="UserId">
             <xsl:text>12345</xsl:text>
           </xsl:element> 

          <xsl:element name="Group">   

            <!--Script to get format as required-->
             <xsl:element name ="RefNum">
              <xsl:value-of select= "<![CDATA[Group/RefNum"]]>/>
               </xsl:element> 

             <xsl:element name ="Id">
              <xsl:value-of select="Group/Id"/>
            </xsl:element>   

              <xsl:element name="GroupNum"> 
            <xsl:element name="GroupNum">                              
                 <xsl:copy-of select="<![CDATA[Group/GroupNum/GroupNum"]]>/>            
            </xsl:element> 
            </xsl:element> 

         </xsl:element> 
          </xsl:element>     
      </xsl:template>
    </xsl:stylesheet>

0 个答案:

没有答案