当前位置: 首页 > 知识库问答 >
问题:

XSL将多个错误节点聚集到一个节点中

太叔俊侠
2023-03-14
<Envelope>
  <Body>
    <Response>
      <return>
        <contact_main>
          <firstname>John</firstname>
          <lastname>Doe</lastname>
          <error>false</error>
          <errors/>
        </contact_main>
        <contact_info1>
          <address1>High Road 748</address1>
          <zip>N17 0AP</zip>
          <city>London</city>
          <country_name>England</country_name>
          <error>true</error>
          <errors>
            <item>
              <text>Some error text here</text>
            </item>
          </errors>
        </contact_info1>
        <contact_card>
          <number>12345678</number>
          <status>Expired</status>
          <valid_to>2010-01-02Z</valid_to>
          <valid>false</valid>
          <error>true</error>
          <errors>
            <item>
              <text>Card is not valid.</text>
            </item>
          </errors>
        </contact_card>
      </return>
    </Response>
    <account_name>No name</account_name>
    <number>12345678</number>
  </Body>
</Envelope>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <xsl:template match="Response">
    <response>
      <contact>
        <xsl:copy-of select="return/contact_main/node()"/>
        <xsl:copy-of select="return/contact_info1/node()"/>
      </contact>
      <card>
        <xsl:copy-of select="return/contact_card/node()"/>
      </card>
    </response>
  </xsl:template>

  <xsl:template match="account_name"/>
  <xsl:template match="number"/>

</xsl:stylesheet>
<response>
  <contact>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
    <error>false</error>
    <errors />

    <address1>High Road 748</address1>
    <zip>N17 0AP</zip>
    <city>London</city>
    <country_name>England</country_name>
    <error>true</error>
    <errors>
      <item>
        <text>Some error text here</text>
      </item>
    </errors>
  </contact>
  <card>
    <number>12345678</number>
    <status>Expired</status>
    <valid_to>2010-01-02Z</valid_to>
    <valid>false</valid>
    <error>true</error>
    <errors>
      <item>
        <text>Card is not valid.</text>
      </item>
    </errors>
  </card>
</response>
<response>
  <contact>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
    <address1>High Road 748</address1>
    <zip>N17 0AP</zip>
    <city>London</city>
    <country_name>England</country_name>
  </contact>
  <card>
    <number>12345678</number>
    <status>Expired</status>
    <valid_to>2010-01-02Z</valid_to>
    <valid>false</valid>
  </card>
  <error>true</error>
  <errors>
    <text>Some error text here</text>
    <text>Card is not valid.</text>
  </errors>
</response>

共有1个答案

令狐宣
2023-03-14

通过xsl:copy-of复制时,可以显式排除错误节点,然后对整个文档中的error节点进行计数,如果计数大于零,则呈现“true”。

XLST的简短示例:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" encoding="utf-8" indent="yes"/>

      <xsl:template match="Response">
        <response>
          <contact>
            <xsl:copy-of select="return/contact_main/node()[local-name() != 'error' and local-name() != 'errors']"/>
            <xsl:copy-of select="return/contact_info1/node()[local-name() != 'error' and local-name() != 'errors']"/>
          </contact>
          <card>
            <xsl:copy-of select="return/contact_card/node()[local-name() != 'error' and local-name() != 'errors']"/>
          </card>

          <error><xsl:value-of select="count(return/*/error[text() = 'true']) &gt; 0"/></error>

          <errors>
             <xsl:for-each select="return/*/errors/item">
                 <xsl:copy-of select="text"/>
             </xsl:for-each>
          </errors>
        </response>
      </xsl:template>

      <xsl:template match="account_name"/>
      <xsl:template match="number"/>

    </xsl:stylesheet>

输出:

    <?xml version="1.0" encoding="utf-8"?>


        <response>
    <contact>
              <firstname>John</firstname>
              <lastname>Doe</lastname>



              <address1>High Road 748</address1>
              <zip>N17 0AP</zip>
              <city>London</city>
              <country_name>England</country_name>


            </contact>
    <card>
              <number>12345678</number>
              <status>Expired</status>
              <valid_to>2010-01-02Z</valid_to>
              <valid>false</valid>


            </card>
    <error>true</error>
    <errors>
    <text>Some error text here</text>
    <text>Card is not valid.</text>
    </errors>
    </response>
 类似资料: