XSLT转换验证XSI:type(或)元素节点是否存在

时间:2019-06-26 06:55:35

标签: xslt

我有一个输入XML,如果元素节点存在'ns1:getGenResponse'(或使用'multiRef'元素的xsi:type = "Gen"进行验证),则需要对其进行过滤

如果任一条件成功,那么我可以处理'multiRef'条记录。

输入XML:

<soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
  <soapenv:Body>
    <ns1:getGenResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://service.pen.eewe.en.de>
      <ns1:getGenReturn xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:Array" soapenc:arrayType="xsd:anyType[2]">
        <item href="#id0" />
        <item href="#id1" />
      </ns1:getGenReturn>
    </ns1:getGenResponse>
    <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Gen">
      <name xsi:type="xsd:string">ulm</name>
      <mail xsi:type="xsd:string">ulm@gmail.com</mail>
    </multiRef>
    <multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Gen">
      <name xsi:type="xsd:string">ABC</name>
      <mail xsi:type="xsd:string">abc@gmail.com</mail>
    </multiRef>
  </soapenv:Body>
</soapenv:Envelope>

尝试使用节点存在'ns1:getGenResponse':

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:response="http://tempuri.org/"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  exclude-result-prefixes="soap response"
>
  <!-- Output -->
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <getGenResponse>
      <xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
        <getGenReturn>
          <name>
            <xsl:value-of select="name" />
          </name>
          <mail>
            <xsl:value-of select="mail" />
          </mail>
        </getGenReturn>
      </xsl:for-each>
    </getGenResponse>
  </xsl:template>
</xsl:stylesheet>    

使用此XSLT,我可以生成blank。我无法生成我的愿望输出。

我也在尝试使用xmulti:type'multiRef'元素节点的数据来验证数据 使用下面的代码,但是我无法执行XSLT

**Trying to validate with xsi:type ="Gen"**

    <xsl:stylesheet version="1.0" xmlns:response="http://tempuri.org/" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="soap response">

        <!-- Output -->
        <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
        <xsl:strip-space elements="*"/>

        <xsl:template match="/">
          <getGenResponse>
                    <xsl:for-each select="//soap:Body/multiRef[substring- after(@xsi:type, ':')='Gen']">
              <getGenReturn>
                <name><xsl:value-of select="name"/></name>
                <mail><xsl:value-of select="mail"/></mail>
              </getGenReturn>
            </xsl:for-each>
          </getGenResponse>
        </xsl:template>
    </xsl:stylesheet>

预期输出:

 **Output Expected**
    <?xml version="1.0" encoding="UTF-8"?>
    <getGenResponse>
       <getGenReturn>
          <name> ULM </name>
          <mail>ulm@gmail.com<mail>
       </getGenReturn>
      <getGenReturn>
          <name>ABC</name>
          <mail>abc@gmail.com<mail>
      </getGenReturn>
    /getGenResponse>

非常感谢您。

1 个答案:

答案 0 :(得分:0)

是的,我在XSLT中声明了xmlns:ns1 =“ http://service.pen.eewe.en.de”。现在我可以执行具有期望结果的代码。

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:response="http://tempuri.org/"
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  exclude-result-prefixes="soap response" xmlns:ns1="http://service.pen.eewe.en.de"
>
  <!-- Output -->
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*" />
  <xsl:template match="/">
    <getGenResponse>
      <xsl:for-each select="//soap:Body[ns1:getGenResponse]/multiRef">
        <getGenReturn>
          <name>
            <xsl:value-of select="name" />
          </name>
          <mail>
            <xsl:value-of select="mail" />
          </mail>
        </getGenReturn>
      </xsl:for-each>
    </getGenResponse>
  </xsl:template>
</xsl:stylesheet>