[英]XSLT1.0 :: I want to add a new element in XML with suffix and namespace as its parent
Solution Required only in XSLT1.0.解决方案 仅在 XSLT1.0 中需要。 I want to create a single xslt which will work add an element irrespective of namespace at fixed position.
我想创建一个单独的 xslt,它可以在固定位置添加一个元素,而不管命名空间如何。 I want to add sas_api_key element as seen in the output of both the XMLs
我想添加sas_api_key元素,如两个 XML 的输出中所见
Input of XML 1: 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>
Output of XML 1: 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>
Input of XML2: 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>
Output of XML2: 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>
Below is the XSLT which I have created but its not working as expected.下面是我创建的 XSLT,但它没有按预期工作。 Please assist.
请协助。
<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>
In XSLT 1.0, you could do:在 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>
Note that this assumes that the parameters
element does have a prefix.请注意,这假定
parameters
元素确实有前缀。
If you have a list of allowed prefixes and namespaces that the parameters
element can use, it would be much better to use it, instead of relying on it being the only element with this local name.如果您有一个
parameters
元素可以使用的允许前缀和命名空间的列表,最好使用它,而不是依赖它作为唯一具有此本地名称的元素。 After all, the very purpose of using a namespace is to distinguish between elements having the same local name.毕竟,使用命名空间的真正目的是区分具有相同本地名称的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.