我一直试图使用XSLT从下面的xml文件中获取元素"test“的内容,但我真的被阻止了。
请您知道如何使用XSLT获得它吗?
XML文件内容如下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<BusinessResponse>
<BusinessResult><![CDATA[<?xml version="1.0" encoding="UTF-8"?><test>helloWorld</test>]]></BusinessResult>
</BusinessResponse>
</soap:Body>
</soap:Envelope>
发布于 2018-08-29 00:55:22
使用XSLT3.0,您可以使用parse-xml()
函数将文本解析为XML,然后可以将XPath放入结构中以获得<test>
元素或它的/test/text()
文本节点:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<xsl:sequence select="parse-xml(/soap:Envelope/soap:Body/BusinessResponse/BusinessResult)/test"/>
</xsl:template>
</xsl:stylesheet>
使用XSLT1.0或更高版本,如果您的内容真的那么简单,并且您只想要<test>
元素中的文本节点,则可以使用substring-before()
和substring-after()
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<xsl:value-of
select="substring-before(
substring-after(/soap:Envelope/soap:Body/BusinessResponse/BusinessResult,
'<test>'),
'</test>')" />
</xsl:template>
</xsl:stylesheet>
如果您需要能够执行一些更复杂的操作,并且需要XSLT和XPath的全部功能,那么您可以通过两个转换来实现这一点。第一个转换将text()
的BusinessResult
序列化为XML,方法是使用xsl:value-of
和disable-output-escaping="yes"
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<xsl:value-of select="/soap:Envelope/soap:Body/BusinessResponse/BusinessResult" disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
它产生:
<?xml version="1.0" encoding="UTF-8"?><test>helloWorld</test>
如果在处理SOAP信封时希望使用其他XML结构,则可能需要排除XML声明:
substring-after(/soap:Envelope/soap:Body/BusinessResponse/BusinessResult,'?>')
然后使用第二个XSLT从XML输出中选择和处理所需的内容。
发布于 2018-08-28 16:25:26
就像这样:
<xsl:value-of select="BusinessResult" disable-output-escaping="yes"/>
https://stackoverflow.com/questions/52060698
复制相似问题