[英]How can I replace an ID element AND all of its reference elements in an XML file using XSLT1.0
[英]XSLT1.0 :: I want to add a new element in XML with suffix and namespace as its parent
解決方案 僅在 XSLT1.0 中需要。 我想創建一個單獨的 xslt,它可以在固定位置添加一個元素,而不管命名空間如何。 我想添加sas_api_key元素,如兩個 XML 的輸出中所見
XML 1 的輸入:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:get="http://tempuri.org/getScreeningProfile">
<get:getScreeningProfile>
<get:parameters>
<get:party_number>334857</get:party_number>
</get:parameters>
</get:getScreeningProfile>
</soap:Body>
</soap:Envelope>
XML 1 的輸出:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:get="http://tempuri.org/getScreeningProfile">
<get:getScreeningProfile>
<get:parameters>
<get:sas_api_key>123<get:sas_api_key>
<get:party_number>334857</get:party_number>
</get:parameters>
</get:getScreeningProfile>
</soap:Body>
</soap:Envelope>
XML2 的輸入:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:biw="http://www.sas.com/xml/namespace/biwebservices">
<soapenv:Header/>
<soapenv:Body>
<biw:getScreeningProfile_v2>
<biw:parameters>
<biw:sas_api_key>123<biw:sas_api_key>
<biw:party_number>12345</biw:party_number>
</biw:parameters>
</biw:getScreeningProfile_v2>
</soapenv:Body>
</soapenv:Envelope>
XML2 的輸出:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:biw="http://www.sas.com/xml/namespace/biwebservices">
<soapenv:Header/>
<soapenv:Body>
<biw:getScreeningProfile_v2>
<biw:parameters>
<biw:sas_api_key>123<biw:sas_api_key>
<biw:party_number>12345</biw:party_number>
</biw:parameters>
</biw:getScreeningProfile_v2>
</soapenv:Body>
</soapenv:Envelope>
下面是我創建的 XSLT,但它沒有按預期工作。 請協助。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="//*[local-name()='parameters']/*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[local-name()='parameters']/*">
<xsl:element name="sas_api_key" inherit-namespaces="yes">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在 XSLT 1.0 中,您可以執行以下操作:
<xsl:template match="*[local-name()='parameters']">
<xsl:copy>
<xsl:element name="{substring-before(name(), ':')}:sas_api_key" namespace="{namespace-uri()}">123</xsl:element>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
請注意,這假定parameters
元素確實有前綴。
如果您有一個parameters
元素可以使用的允許前綴和命名空間的列表,最好使用它,而不是依賴它作為唯一具有此本地名稱的元素。 畢竟,使用命名空間的真正目的是區分具有相同本地名稱的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.