借助xslt将xml节点复制到一个位置到另一个位置

时间:2014-05-30 11:44:56

标签: xml xslt

我需要转换以下xml。

这是我的源xml:

<DST>
  <DST>
    <PQAWD>
      <username>#username#</username>
      <password>#password#</password>
      <newPassword/>
    </PQAWD>
  </DST>
  <CacheRequest>
    <getCache AWDConfig="Y">
      <useClientName>N</useClientName>
      <useGlobalCache>Y</useGlobalCache>
      <keyName>AWDConfig</keyName>
      <targetNode>.</targetNode>
    </getCache>
  </CacheRequest>
  <AWDConfig>
    <IPAddress>$host-address$</IPAddress>
    <XMLPort>$host-port$</XMLPort>
    <scriptHostName>######</scriptHostName>
  </AWDConfig>
</DST>

说明:

必须将上述XML转换为所需的输出,如下所示。 这些是我必须转换的节点:

  • //AWDConfig/IPAddress应移至//DST/PQAWD下并重命名为hostAddress
  • //AWDconfig/XMLPort应移至//DST/PQAWD下并重命名为hostPort

期望的输出:

<DST>
  <DST>
    <PQAWD>
      <username>#username#</username>
      <password>#password#</password>
      <newPassword/>                
     <hostAddress>$host-address$</hostAddress>     
     <hostPort>$host-port$</hostPort>
    </PQAWD>
  </DST>
  <CacheRequest>
    <getCache AWDConfig="Y">
      <useClientName>N</useClientName>
      <useGlobalCache>Y</useGlobalCache>
      <keyName>AWDConfig</keyName>
      <targetNode>.</targetNode>
    </getCache>
  </CacheRequest>
  <AWDConfig>
    <IPAddress>$host-address$</IPAddress>
    <XMLPort>$host-port$</XMLPort>
  </AWDConfig>
</DST>

2 个答案:

答案 0 :(得分:0)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="newPassword">
<xsl:value-of select="parent::DST/AWDConfig/IPAddres"/>
<xsl:copy/>
<hostAddress><xsl:value-of select="../../../AWDConfig/IPAddress"/></hostAddress>     
<hostPort><xsl:value-of select="../../../AWDConfig/XMLPort"/></hostPort>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

尝试以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="//DST/PQAWD">
      <PQAWD>
         <xsl:apply-templates select="@*|node()" />
         <hostAddress>
            <xsl:value-of select="//AWDConfig/IPAddress" />
         </hostAddress>
         <hostPort>
            <xsl:value-of select="//AWDConfig/XMLPort" />
         </hostPort>
      </PQAWD>
   </xsl:template>
</xsl:stylesheet>

<强>输出:

<?xml version="1.0" encoding="UTF-8"?>
<DST>
   <DST>
      <PQAWD>
         <username>#username#</username>
         <password>#password#</password>
         <newPassword />
         <hostAddress>$host-address$</hostAddress>
         <hostPort>$host-port$</hostPort>
      </PQAWD>
   </DST>
   <CacheRequest>
      <getCache AWDConfig="Y">
         <useClientName>N</useClientName>
         <useGlobalCache>Y</useGlobalCache>
         <keyName>AWDConfig</keyName>
         <targetNode>.</targetNode>
      </getCache>
   </CacheRequest>
   <AWDConfig>
      <IPAddress>$host-address$</IPAddress>
      <XMLPort>$host-port$</XMLPort>
      <scriptHostName>######</scriptHostName>
   </AWDConfig>
</DST>