在XSLT

时间:2018-08-27 08:51:04

标签: xml xslt

我有以下XML,我想获得ID和AccountData的状态具有最大匹配数。因此结果应包含id = 1000349827和status = trusted。如何在XSLT v1中做到这一点:

<Body>
<LookupCustomersRs>
    <CustomersData>
    <CustomerData objectID="0" objectStatus="10">
        <AccountData objectID="0" objectStatus="1" objectType="ESIA_ACCOUNT">
            <Id>1000349826</Id>
            <status>trusted</status>
            <Matches>
                <Match>doc</Match>
                <Match>mobile</Match>
            </Matches>
        </AccountData>
        <AccountData objectID="0" objectStatus="1" objectType="ESIA_ACCOUNT">
            <Id>1000349827</Id>
            <status>trusted</status>
            <Matches>
                <Match>doc</Match>
                <Match>snils</Match>
                <Match>mobile</Match>
                <Match>mobile2</Match>
            </Matches>
        </AccountData>
        <AccountData objectID="0" objectStatus="1" objectType="ESIA_ACCOUNT">
            <Id>1000349828</Id>
            <status>trusted</status>
            <Matches>
                <Match>doc</Match>
            </Matches>
        </AccountData>
    </CustomerData>
    </CustomersData>
</LookupCustomersRs>

1 个答案:

答案 0 :(得分:0)

根据马丁回答的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<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="text" indent="yes"/>
    <xsl:template match="/">
      <xsl:apply-templates select="Body/LookupCustomersRs/CustomersData/CustomerData/AccountData">
        <xsl:sort select="count(Matches/Match)" order="descending"/>
      </xsl:apply-templates>
    </xsl:template>
  <xsl:template match="Body/LookupCustomersRs/CustomersData/CustomerData/AccountData">
    <xsl:if test="position()=1">
      <xsl:text>Id=</xsl:text>
      <xsl:value-of select="Id"/>
      <xsl:text>,</xsl:text>
      <xsl:text>status=</xsl:text>
      <xsl:value-of select="status"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>