使用XSLT修改XML

时间:2015-08-20 12:10:37

标签: xml xslt

我是XSLT的新手。我有以下xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP:Body>
      <response:performJobResponse xmlns:response="http://tempuri.org/" SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <whiteboard>
            <PQ>
               <response>
                  <PQIssueHistory>
                     <AWD10SP7_1orhigher>Y</AWD10SP7_1orhigher>
                     <hostAddress>10.193.XXX.78</hostAddress>
                     <hostPort>12955</hostPort>
                     <userID>7X606</userID>
                     <password>Qfgjf@123</password>
                     <issue>
                        <CRDATTIM>2015-07-29-04.27.15.461040</CRDATTIM>
                        <RECORDCD>T</RECORDCD>
                        <CRNODE>01</CRNODE>
                        <ORIGUSERID>DT77214</ORIGUSERID>
                     </issue>
                  </PQIssueHistory>
                  <results>
                     <row>
                        <RECTYP>HISTORY</RECTYP>
                     </row>
                  </results>
                  <comments xmlns="http://www.dsttechnologies.com/awd/rest/v1" total="4">
                     <comment notify="false" id="b5ee129a-0338-41c7-881b-ab8c1727de36">
                        <source>SYSTEM</source>
                     </comment>
                  </comments>
               </response>
            </PQ>
         </whiteboard>
         <jobReturn>
            <taskName>TransformNode</taskName>
            <description>TransformNode Succeeded</description>
            <value>0</value>
         </jobReturn>
      </response:performJobResponse>
   </SOAP:Body>
</SOAP:Envelope>

我想修改上面的xml,使<comments>下的节点移动到<PQIssueHistory>下。新移动的<comment>应重命名为<row>

最终得到的xml应该如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP:Body>
      <response:performJobResponse xmlns:response="http://tempuri.org/" SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <whiteboard>
            <PQ>
               <PQIssueHistory>
                  <AWD10SP7_1orhigher>Y</AWD10SP7_1orhigher>
                  <hostAddress>10.193.XXX.78</hostAddress>
                  <hostPort>12955</hostPort>
                  <userID>7X606</userID>
                  <password>Qfgjf@123</password>
                  <issue>
                     <CRDATTIM>2015-07-29-04.27.15.461040</CRDATTIM>
                     <RECORDCD>T</RECORDCD>
                     <CRNODE>01</CRNODE>
                     <ORIGUSERID>DT77214</ORIGUSERID>
                  </issue>
               </PQIssueHistory>
               <row>
                  <RECTYP>HISTORY ONE</RECTYP>
               </row>
               <row>
                  <RECTYP>HISTORY TWO</RECTYP>
               </row>
               <row>
                  <source>SYSTEM</source>
               </row>
            </PQ>
         </whiteboard>
         <jobReturn>
            <taskName>TransformNode</taskName>
            <description>TransformNode Succeeded</description>
            <value>0</value>
         </jobReturn>
      </response:performJobResponse>
   </SOAP:Body>
</SOAP:Envelope>

如果您有任何问题,请随时询问。

1 个答案:

答案 0 :(得分:1)

怎么样......

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"
    version="2.0"
    exclude-result-prefixes="v">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="results">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
    <xsl:apply-templates select="../../response/v:comments/v:comment" />
  </xsl:copy>
</xsl:template>

<xsl:template match="v:comments" />

<xsl:template match="v:comment">
  <row>
    <xsl:apply-templates mode="sans-namespace" />
  </row>
</xsl:template>

<xsl:template match="*" mode="sans-namespace">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>