XSLT转换
您好,我无法遍历输入XML文件以获得所需的输出。请帮忙。 期望输出:XSLT转换,xslt,Xslt,您好,我无法遍历输入XML文件以获得所需的输出。请帮忙。 期望输出: <DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport> 我的XSLT文件: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xm
<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport>
我的XSLT文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rout="http://safe.com/RoutePlanner/" xmlns:dp="http://www.datapower.com/extensions" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="/Envelope/Body/ABCCustomMsg/*"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输入XML:
<Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/">
<Body>
<rout:ABCCustomMsg
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
>
<DocFWImport><Header senderID="ABC1234"/></Request></DocFWImport></ns1:ABCCustomMsg>
</Body>
</Envelope>
您需要在XPath中使用名称空间前缀。这就是他们的目的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rout="http://safe.com/RoutePlanner/"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这将产生以下输出:
<DocFWImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:rout="http://safe.com/RoutePlanner/"><Header senderID="ABC1234"/></Request></DocFWImport>
如果确实要从要复制的元素中删除名称空间(因为示例输出没有名称空间),可以这样做:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rout="http://safe.com/RoutePlanner/"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
extension-element-prefixes="dp" exclude-result-prefixes="dp">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="/soapenv:Envelope/soapenv:Body/rout:ABCCustomMsg/*"/>
</xsl:template>
<xsl:template match="@*|text()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
这将产生您所显示的所需输出。您的元素位于名称空间中,但您没有选择名称空间中的元素。例如,请参见您的输入和输出都不是格式良好的XML文档。