2

我创建了一个与各种服务器上的 Web 服务对话的客户端,并尝试使用 XSL 格式化 XML。这些服务都是根据相同的规范创建的,因此它们发出的 XML 具有相同的格式和 xmlns 命名空间名称。我的 XSL 需要测试特定类型的元素以了解如何格式化它们。问题是在服务器之间,它们使用不同的 xmlns 前缀。我一直在使用的测试是纯字符串比较,并没有将前缀扩展为完整的 xmlns,因此比较失败。我的问题是,有没有办法扩展前缀以便字符串比较起作用?

这是一个简化的例子来说明问题。在“xsl:if test”行中,我需要将“r0”和“xx”扩展为“ http://www.foo.com/2.1/schema ”,以便它们测试相同:

XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:r0="http://www.foo.com/2.1/schema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="/">
<html>
<head>
  </head>
     <body >
        <xsl:for-each select="MyResp/r0:creditVendReceipt/r0:transactions/r0:tx">
          <xsl:if test="@xsi:type='r0:CreditTx'">
                <xsl:value-of select="r0:amt/@value"/>(CR)
          </xsl:if>
          <xsl:if test="@xsi:type='r0:DebitTx'">
                <xsl:value-of select="r0:amt/@value"/>(DB)
          </xsl:if>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

XML(这有效):

<?xml version="1.0" encoding="unicode"?>
<MyResp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:r0="http://www.foo.com/2.1/schema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<r0:creditVendReceipt receiptNo="1234567890">
    <r0:transactions>
        <r0:tx xsi:type="r0:CreditTx">
            <r0:amt value="100" />
        </r0:tx>
        <r0:tx xsi:type="r0:DebitTx">
            <r0:amt value="50" />
        </r0:tx>
    </r0:transactions>
</r0:creditVendReceipt>
</MyResp>

XML(失败):

<?xml version="1.0" encoding="unicode"?>
<MyResp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xx="http://www.foo.com/2.1/schema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xx:creditVendReceipt receiptNo="1234567890">
    <xx:transactions>
        <xx:tx xsi:type="xx:CreditTx">
            <xx:amt value="100" />
        </xx:tx>
        <xx:tx xsi:type="xx:DebitTx">
            <xx:amt value="50" />
        </xx:tx>
    </xx:transactions>
</xx:creditVendReceipt>
</MyResp>
4

2 回答 2

3

在 XSLT 2.0 中你可以做

<xsl:if test='resolve-QName(@xsi:type, .) = 
              QName("http://www.foo.com/2.1/schema", "CreditTx")'>

一个比 JLRishe 的 XSLT 1.0 更完整的解决方案是这样的

<xsl:if test="substring-after(@xsi:type, ':') = 'CreditTx' and 
                 namespace::*[name()=substring-before(@xsi:type, ':')] =     
                    'http://www.foo.com/2.1/schema'">
于 2013-03-21T14:02:41.713 回答
0

在 XSLT 2.0 中你可以做

<xsl:if test='resolve-QName(@xsi:type, .) = 
              QName("http://www.foo.com/2.1/schema", "CreditTx")'>
于 2013-03-21T13:57:49.177 回答