我需要转换以下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>
答案 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>