使用xslt复制节点和修改节点

时间:2013-08-22 13:51:58

标签: xml xslt copy

这是我输入的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SenderID="dummySenderID" TimeStamp="2008-12-04T01:00:40+09:00" Version="1.3">
   <!--Generated by the CIP4 Java open source JDF Library version : CIP4 JDF Writer Java 1.3 BLD 37-->
   <Response ID="Rdummy" Type="Status" refID="dummy" xsi:type="SignalStatus">
      <DeviceInfo DeviceCondition="OK" DeviceID="KMBT24BCA6" DeviceStatus="Idle" StatusDetails="Ready">
         <SignalQueueStatus>
            <Queue>
               <QueueEntry>
                  <JobPhase Amount="3" PercentCompleted="100" PhaseStartTime="2008-12-04T01:00:35+09:00" QueueEntryID="JDF55937313" RestTime="PT00M" StartTime="2008-12-04T01:00:35+09:00" Status="Completed" StatusDetails="JobCompletedSuccessfully" TotalAmount="3" />
               </QueueEntry>
            </Queue>
         </SignalQueueStatus>
      </DeviceInfo>
   </Response>
</JMF>

我想将此xml转换为如下:

<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    SenderID="dummySenderID" TimeStamp="2008-12-04T01:00:40+09:00" Version="1.3">
    <Response ID="Rdummy" Type="Status" refID="dummy" xsi:type="SignalStatus">
        <DeviceInfo DeviceCondition="OK" DeviceID="KMBT24BCA6"
            DeviceStatus="Idle" StatusDetails="Ready">
            <SignalQueueStatus>
                <Queue>
                    <QueueEntry>
                        <JobPhase Amount="3" PercentCompleted="100"
                            PhaseStartTime="2008-12-04T01:00:35+09:00" QueueEntryID="JDF55937313"
                            RestTime="PT00M" StartTime="2008-12-04T01:00:35+09:00" Status="Completed"
                            StatusDetails="JobCompletedSuccessfully" TotalAmount="3" />
                    </QueueEntry>
                </Queue>
            </SignalQueueStatus>
        </DeviceInfo>
        <!-- newly added xml node Start-->
        <SignalStatus>
            <DeviceInfo DeviceCondition="OK" DeviceID="KMBT24BCA6"
                DeviceStatus="Idle" StatusDetails="Ready">

                <JobPhase Amount="3" PercentCompleted="100"
                    PhaseStartTime="2008-12-04T01:00:35+09:00" QueueEntryID="JDF55937313"
                    RestTime="PT00M" StartTime="2008-12-04T01:00:35+09:00" Status="Completed"
                    StatusDetails="JobCompletedSuccessfully" TotalAmount="3" />

            </DeviceInfo>
        </SignalStatus>
        <!-- newly added xml node End-->
    </Response>
</JMF>

2 个答案:

答案 0 :(得分:0)

从你的例子来看,这可能就是这样,但是你似乎并没有明确表达你真正的问题

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:x="http://www.CIP4.org/JDFSchema_1_1">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="x:DeviceInfo">
    <xsl:copy-of select="."/>
    <SignalStatus>
      <xsl:copy-of select="@*"/>
      <xsl:copy-of select="x:SignalQueueStatus/x:Queue/x:QueueEntry/x:JobPhase"/>
    </SignalStatus>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

我的解决方案是@jvverde非常相似的解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.CIP4.org/JDFSchema_1_1" xmlns:x="http://www.CIP4.org/JDFSchema_1_1" exclude-result-prefixes="x">
    <xsl:output omit-xml-declaration="no" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="x:DeviceInfo">
        <xsl:copy-of select="."/>
        <!-- newly added xml node-->
        <xsl:comment>newly added xml node start</xsl:comment>
        <SignalStatus>
            <DeviceInfo>
                <xsl:copy-of select="@*"/>
                <xsl:copy-of select="x:SignalQueueStatus/x:Queue/x:QueueEntry/x:JobPhase" />
            </DeviceInfo>
        </SignalStatus>
        <xsl:comment>newly added xml node start end</xsl:comment>
    </xsl:template>

</xsl:stylesheet>