我是XSLT的新手,请您告诉我用于将下面的输入转换为输出XML的XSLT代码吗?
这是我的输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
<n:Header>
</n:Header>
<n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ns1:sayHello xmlns:ns1="http://webservice_product/helloworld">
<toWhom>Micky</toWhom>
<toMe>123</toMe>
<objAs>
<id>323232</id>
</objAs>
</ns1:sayHello>
</n:Body>
</n:Envelope>
这是我想要的输出XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
<n:Header>
</n:Header>
<n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ns1:sayHello xmlns:ns1="http://webservice_product/helloworld" xsi:type="ns698:Product" xmlns:ns698="urn:objects.prodcuts.com">
<toWhom>Micky</toWhom>
<toMe>123</toMe>
<objAs>
<id>323232</id>
</objAs>
</ns1:sayHello>
</n:Body>
</n:Envelope>
我努力实现所需的输出XML,但它对我没有帮助。此外,Stack Overflow不允许我粘贴所有的XSL代码。
答案 0 :(得分:3)
由于xsi:type
属性使用前缀xsi
而没有任何声明,因此您所需的XML输出不是格式良好的命名空间。假设您要在ns1:sayHello
元素上添加该声明,您可以使用
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://webservice_product/helloworld"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns1:sayHello">
<ns1:sayHello xsi:type="ns698:Product" xmlns:ns698="urn:objects.prodcuts.com">
<xsl:apply-templates select="@* | node()"/>
</ns1:sayHello>
</xsl:template>
</xsl:stylesheet>
当我将带有Saxon 6.5.5的样式表应用于输入
时<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="tutorials.xsl"?>
<n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
<n:Header>
</n:Header>
<n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ns1:sayHello xmlns:ns1="http://webservice_product/helloworld">
<toWhom>Micky</toWhom>
<toMe>123</toMe>
<objAs>
<id>323232</id>
</objAs>
</ns1:sayHello>
</n:Body>
</n:Envelope>
我得到了结果
<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="tutorials.xsl"?><n:Envelope xmlns:n="http://schemas.xmlsoap.org/soap/envelope/">
<n:Header>
</n:Header>
<n:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<ns1:sayHello xmlns:ns1="http://webservice_product/helloworld" xmlns:ns698="urn:objects.prodcuts.com" xmlns:xsi="h
ttp://www.w3.org/2001/XMLSchema-instance" xsi:type="ns698:Product">
<toWhom>Micky</toWhom>
<toMe>123</toMe>
<objAs>
<id>323232</id>
</objAs>
</ns1:sayHello>
</n:Body>
</n:Envelope>