我有這個 XML,我想添加一個帶有硬編碼值的新元素名稱。我怎樣才能實作它?
XML:
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
期望輸出
<?xml version="1.0" encoding="UTF-8"?><m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<!--<xsl:output method="xml" encoding="utf-8" indent="yes"/>-->
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<m2:overrideScriptName>CM-PrMtActMg</m2:overrideScriptName>
</xsl:stylesheet>
要在 custDetails 之前添加的新元素
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
uj5u.com熱心網友回復:
請嘗試以下解決方案。
它使用所謂的身份轉換模式。
輸入 XML
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
XSLT 1.0
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m2:custDetails">
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
輸出 XML
<?xml version='1.0' encoding='utf-8' ?>
<m2:InvokeWebService xmlns:m2="http://www.w3.org/2001/XMLSchema-instance">
<m2:request>
<m2:action>ADD</m2:action>
<m2:commonDetails>
<m2:needSupport>Y</m2:needSupport>
</m2:commonDetails>
<m2:overrideScriptName>NewScript</m2:overrideScriptName>
<m2:custDetails>
<m2:name>Tony,Hawk</m2:name>
<m2:accountNumber>23232423566</m2:accountNumber>
<m2:sensitiveCustomer>Y</m2:sensitiveCustomer>
</m2:custDetails>
</m2:request>
</m2:InvokeWebService>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365393.html
