XSLT无法获取嵌套节点的值

时间:2019-12-13 11:37:52

标签: xml xslt

我有以下xml:

<?xml version="1.0" encoding="UTF-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <IsAliveResponse xmlns="http://tempuri.org/"> <IsAliveResult xmlns:a="http://schemas.datacontract.org/2004/07/Regulatory.Compliance.Screening.DTOs.MessageContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <CorrelationId xmlns="http://schemas.datacontract.org/2004/07/Services.Common">4cd91989-1142-4a9c-8f40-149a47078c90</CorrelationId> <ErrorMessages xmlns="http://schemas.datacontract.org/2004/07/Services.Common" xmlns:b="http://schemas.datacontract.org/2004/07/Services.Common.Models"/> <Status xmlns="http://schemas.datacontract.org/2004/07/Services.Common">Success</Status> </IsAliveResult> </IsAliveResponse> </s:Body> </s:Envelope>

我需要通过XSLT转换获取 Status 节点的值。不管我尝试什么,我都无法深入s:body节点。

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://schemas.datacontract.org/2004/07/Regulatory.Compliance.Screening.DTOs.MessageContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Services.Common" > <xsl:template match="/"> <xsl:value-of select="/s:Envelope/s:Body" /> </xsl:template> </xsl:stylesheet>

如果我将xsl:value-of select更改为/s:Envelope/s:Body/IsAliveResponse/IsAliveResult/Status,则不会返回任何内容。

任何人都可以解释为什么该转换不起作用以及如何成功检索状态值吗?

1 个答案:

答案 0 :(得分:1)

您必须更仔细地观察名称空间-尤其是。默认的。试试:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://tempuri.org/"
xmlns:c="http://schemas.datacontract.org/2004/07/Services.Common">

<xsl:template match="/">
    <xsl:value-of select="/s:Envelope/s:Body/t:IsAliveResponse/t:IsAliveResult/c:Status" />
</xsl:template>

</xsl:stylesheet>