修改xsl copy-of中的节点文本

时间:2014-02-28 23:56:49

标签: xslt

我有一个看起来像

的xml文档
<!-- language: lang-xml -->

<?xml version="1.0" encoding="UTF-8"?>
<Request xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<AccountServer e-dtype="int">3</AccountServer>    
</Header>
<Response>
<ResponseList e-dtype="list">
  <Response>
    <RequestId e-dtype="string">ServiceOrderGetRef</RequestId>
    <RequestObjName e-dtype="string">ServiceOrder</RequestObjName>
    <ServiceOrder>          
      <CreateDt e-dtype="dateTime">2014-03-01 00:00:00</CreateDt>
      <CreateWho e-dtype="string">vc</CreateWho>          
      <WorkflowStartDt e-dtype="dateTime">2014-04-01 00:00:00</WorkflowStartDt>          
    </ServiceOrder>
  </Response>
  <Response>
    <ComponentList e-dtype="list"/>
    <Count e-dtype="int">0</Count>
    <RequestId e-dtype="string">ComponentFindRef</RequestId>
    <RequestObjName e-dtype="string">Component</RequestObjName>
    <TotalCount e-dtype="int">0</TotalCount>
  </Response>
  <Response>
    <Count e-dtype="int">0</Count>
    <CustomerContractList e-dtype="list"/>
    <RequestId e-dtype="string">CustomerContractRef</RequestId>
    <RequestObjName e-dtype="string">CustomerContract</RequestObjName>
    <TotalCount e-dtype="int">0</TotalCount>
  </Response>
  <Response>
    <Count e-dtype="int">0</Count>
    <ProductList e-dtype="list"/>
    <RequestId e-dtype="string">ProductRef</RequestId>
    <RequestObjName e-dtype="string">Product</RequestObjName>
    <TotalCount e-dtype="int">0</TotalCount>
  </Response>
  <Response>
    <Count e-dtype="int">0</Count>
    <NrcList e-dtype="list"/>
    <RequestId e-dtype="string">NrcFindRef</RequestId>
    <RequestObjName e-dtype="string">Nrc</RequestObjName>
    <TotalCount e-dtype="int">0</TotalCount>
  </Response>
  </ResponseList>
  </Response>
  </Request>

我正在使用copy-of函数将节点ServiceOrder复制到另一个xml文档中

我想修改节点WorkFlowStartDtCreateDt的文字,然后执行copy-of。我怎么能这样做?
在修改文本后,我复制的serviceorder节点应如下所示。下面是结果xml

<?xml version="1.0" encoding="UTF-8"?>
<Request>
 <Header>
  <OperatorName e-dtype="string">ws</OperatorName>
  <ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">
  <LogicalServiceOrder>
    <RequestId e-dtype="string">MyExistingOrder</RequestId>
    <LogicalServiceOrderPreProcess>
      <Fetch e-dtype="boolean">true</Fetch>
      <Order>
        <AccountInternalId e-dtype="int">12345</AccountInternalId>
        <Key>
          <OrderId e-dtype="numeric">12345678</OrderId>
        </Key>
      </Order>
      <ServiceOrderList e-dtype="list">
        <ServiceOrder  xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-   
       instance">          
              <CreateDt e-dtype="dateTime">2014-03-02 00:00:00</CreateDt>
              <CreateWho e-dtype="string">vc</CreateWho>          
              <WorkflowStartDt e-dtype="dateTime">2014-05-01 00:00:00</WorkflowStartDt>          
    </ServiceOrder>
      </ServiceOrderList>
    </LogicalServiceOrderPreProcess>
  </LogicalServiceOrder>
  </RequestList>
</CustomerUdtRequest>
</Request>

下面是我的xslt处理器文件

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>    
<xsl:template match="/">
<Request>
<Header>
<OperatorName e-dtype="string">ws</OperatorName>
<ApplicationName e-dtype="string">ws</ApplicationName>
</Header>
<CustomerUdtRequest>
<RequestList e-dtype="list">                    
<LogicalServiceOrder>
<RequestId e-dtype="string">MyExistingOrder</RequestId>
<LogicalServiceOrderPreProcess>                         <Order>                                         <AccountInternalId e-dtype="int">
<xsl:value-of   
      select="/Request/Response/ResponseList/Response/ServiceOrder/AccountInternalId"/>
                                        </AccountInternalId>                                            <Key>                                           <OrderId e-dtype="numeric">                                                 <xsl:value-of select="/Request/Response/ResponseList/Response/ServiceOrder/OrderId"/>
                                            </OrderId>
                                        </Key>
                                    </Order>
                                    <ServiceOrderList e-dtype="list">                                   
                                        <xsl:copy-of select="/Request/Response/ResponseList/Response/ServiceOrder"/>
                                    </ServiceOrderList>
                                </LogicalServiceOrderPreProcess>
                            </LogicalServiceOrder>
                        </xsl:if>
                    </RequestList>
                </CustomerUdtRequest>
            </Request>
        </xsl:template> 
    </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

  

我想修改节点WorkFlowStartDt和CreateDt的文本,然后执行   副本。

那将是一种不必要的复杂情况。您可以修改节点,而将它们添加到输出树。

由于您基本上希望“按原样”复制除两个节点之外的所有内容,因此最好从身份转换模板开始,然后为两个特定节点添加“异常”模板需要修改(两个模板都有,因为修改是相同的)。:

<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"/>

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

<xsl:template match="CreateDt | WorkFlowStartDt">
    <xsl:copy>
        <xsl:value-of select="concat('**', ., '**')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

加了:

在回答编辑过的问题时,请尝试以下样式表:

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
<Request>
    <Header>
        <OperatorName e-dtype="string">ws</OperatorName>
        <ApplicationName e-dtype="string">ws</ApplicationName>
    </Header>
    <CustomerUdtRequest>
        <RequestList e-dtype="list">                    
            <LogicalServiceOrder>
                <RequestId e-dtype="string">MyExistingOrder</RequestId>
                <LogicalServiceOrderPreProcess>
                    <Order>
                        <AccountInternalId e-dtype="int">
                            <!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
                            <xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:AccountInternalId"/>
                        </AccountInternalId>
                        <Key>
                            <OrderId e-dtype="numeric">
                                <!-- THIS DOESN'T POINT TO ANY EXISTING NODE!! -->
                                <xsl:value-of select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder/ns:OrderId"/>
                            </OrderId>
                        </Key>
                    </Order>

                    <xsl:apply-templates select="ns:Request/ns:Response/ns:ResponseList/ns:Response/ns:ServiceOrder"/>

                </LogicalServiceOrderPreProcess>
            </LogicalServiceOrder>
        </RequestList>
    </CustomerUdtRequest>
</Request>
</xsl:template> 

<xsl:template match="ns:ServiceOrder">
    <xsl:copy>
        <xsl:copy-of select="ns:CreateWho"/>
        <xsl:apply-templates select="ns:CreateDt | ns:WorkflowStartDt"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns:CreateDt | ns:WorkflowStartDt">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
        <xsl:value-of select="concat('**', ., '**')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当应用于您的输入(减去非法开场评论)时,会获得以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<Request>
   <Header>
      <OperatorName e-dtype="string">ws</OperatorName>
      <ApplicationName e-dtype="string">ws</ApplicationName>
   </Header>
   <CustomerUdtRequest>
      <RequestList e-dtype="list">
         <LogicalServiceOrder>
            <RequestId e-dtype="string">MyExistingOrder</RequestId>
            <LogicalServiceOrderPreProcess>
               <Order>
                  <AccountInternalId e-dtype="int"/>
                  <Key>
                     <OrderId e-dtype="numeric"/>
                  </Key>
               </Order>
               <ServiceOrder xmlns="fst" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <CreateWho e-dtype="string">vc</CreateWho>
                  <CreateDt e-dtype="dateTime">**2014-03-01 00:00:00**</CreateDt>
                  <WorkflowStartDt e-dtype="dateTime">**2014-04-01 00:00:00**</WorkflowStartDt>
               </ServiceOrder>
            </LogicalServiceOrderPreProcess>
         </LogicalServiceOrder>
      </RequestList>
   </CustomerUdtRequest>
</Request>