Остановить дубликаты при поиске содержимого () в XSLT - PullRequest
0 голосов
/ 09 ноября 2010

Ладно, я достиг полного ментального блока, пытаясь заставить это работать, и мне было интересно, сможет ли кто-нибудь еще обдумать это.У меня есть следующая структура HTML.

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <listing>
        <name>Frank Spencer</name>
        <dob>2010-09-01</dob>
        <details>
         <firmname>Scotts</firmname>
         <address>Blah Blah</address>
         <businessname>Scotts</businessname>
         <businessaddress>Blahdeblah</businessaddress>
        </details>
        <details>
         <firmname>Scotts</firmname>
         <address>Blah Blah</address>
         <businessname>Wilson and Son</businessname>
         <businessaddress>Blahdeblah</businessaddress>
        </details>
        <details>
         <firmname>Wilson and Son</firmname>
         <address>Blah Blah</address>
         <businessname>Brudebakers</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
       <details>
         <firmname>Carnage and Co.</firmname>
         <address>Blah Blah</address>
         <businessname>Brudebakers</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
     </listing>
      <listing>
       <name>Han Solo</name>
       <dob>2010-09-01</dob>
       <details>
         <firmname>Independent trading</firmname>
         <address>Blah Blah</address>
         <businessname>Fugitive freight</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
       <details>
         <firmname>Scotts</firmname>
         <address>Blah Blah</address>
         <businessname>Wilson and Son</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
       <details>
         <firmname>Wilson and Son</firmname>
         <address>Blah Blah</address>
         <businessname>Scotts</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
       <details>
         <firmname>Carnage and Co.</firmname>
         <address>Blah Blah</address>
         <businessname>Brudebakers</businessname>
         <businessaddress>Blahdeblah</businessaddress>
       </details>
    </listing>
</root>

Со следующим XSLT.

<?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="html" indent="yes"/>

<xsl:param name="searchName">Wilson</xsl:param>
<xsl:param name="searchName2"></xsl:param>

<xsl:variable name="Uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="Lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:template match="/">
    <xsl:for-each select="root/listing">
       <p>Name: <xsl:value-of select="name"/></p>
       <p>DOB: <xsl:value-of select="dob" /></p>

<xsl:for-each select="details[not(firmname=following::root/listing/details/firmname) and not(businessname=following::root/listing/details/businessname) or not(firmname=following::root/listing/details/businessname) or not(businessname=following::root/listing/details/firmname)]"> 

<xsl:choose>

    <xsl:when test="contains(translate(firmname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase)) and contains(translate(businessname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase)) and not($searchName = '')">
 (This individual has previously worked at:
 <xsl:value-of select="firmname" />
 </xsl:when>

 <xsl:when test="contains(translate(businessname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase)) and not(contains(translate(firmname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase))) and not($searchName = '')">
(This individual has previously worked at:
 <xsl:value-of select="businessname" />)
 </xsl:when>

 <xsl:when test="contains(translate(firmname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase)) and not(contains(translate(businessname,$Uppercase,$Lowercase), translate($searchName,$Uppercase,$Lowercase))) and not($searchName = '')">
 (This individual has previously worked at:
 <xsl:value-of select="firmname" />)<br/>
 </xsl:when>

 </xsl:choose>
 </xsl:for-each>
 </xsl:for-each>
 </xsl:template>
 </xsl:stylesheet>

Я хочу получить список фирм, которые содержат поисковый запрос.Но я получаю следующее:

  Name: Frank Spencer

  DOB: 2010-09-01

  (This individual has previously worked at: Wilson and Son)
  (This individual has previously worked at: Wilson and Son)

  Name: Han Solo

  DOB: 2010-09-01

  (This individual has previously worked at: Wilson and Son)
  (This individual has previously worked at: Wilson and Son)

xslt должен посмотреть и название фирмы, и название компании, а затем, если он найдет совпадение с содержимым, он должен распечатать только одну ссылку.Но я не могу заставить его работать.Кто-нибудь получил какие-либо решения / предложения?Идеальный результат есть.

<body>
  <p>Name: Frank Spencer</p>

  <p>DOB: 2010-09-01</p>

  <p>(This individual has previously worked at: Wilson and Son)</p>


  <p>Name: Han Solo</p>

  <p>DOB: 2010-09-01</p>

  <p>(This individual has previously worked at: Wilson and Son)</p>
</body>

Ответы [ 2 ]

1 голос
/ 09 ноября 2010

Эта таблица стилей:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:key name="kNamesByListingAndValue" match="firmname|businessname"
             use="concat(generate-id(../..),'++',.)"/>
    <xsl:param name="searchName" select="'Wilson'"/>
    <xsl:variable name="Uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="Lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:template match="/">
        <xsl:apply-templates select="root/listing
                                           [details
                                            [(firmname|businessname)
                                             [contains(translate(
                                                          .,
                                                          $Uppercase,
                                                          $Lowercase),
                                                       translate(
                                                          $searchName,
                                                          $Uppercase,
                                                          $Lowercase))]]]"/>
    </xsl:template>
    <xsl:template match="listing">
        <p>
            <xsl:value-of select="concat('Name: ',name)"/>
        </p>
        <p>
            <xsl:value-of select="concat('DOB: ',dob)" />
        </p>
        <xsl:apply-templates
             select="details[(firmname|businessname)
                                [contains(translate(.,
                                                    $Uppercase,
                                                    $Lowercase),
                                          translate($searchName,
                                                    $Uppercase,
                                                    $Lowercase))]
                                [count(.|key('kNamesByListingAndValue',
                                             concat(generate-id(current()),
                                                    '++',.))[1])=1]]"/>
    </xsl:template>
    <xsl:template match="details">
        <span style="color:#626466;">
           <xsl:text>(This individual has previously worked at:&#160;</xsl:text>
            <strong>
                <xsl:value-of select="firmname" />
            </strong>
            <xsl:text> [</xsl:text>
            <i>
                <xsl:value-of select="businessname" />
            </i>
            <xsl:text>] )</xsl:text>
        </span>
        <br/>
    </xsl:template>
</xsl:stylesheet>

Вывод:

<p>Name: Frank Spencer</p>
<p>DOB: 2010-09-01</p>
<span style="color:#626466;">(This individual has previously worked at: <strong>Scotts</strong> [<i>Wilson and Son</i>] )</span>
<br />
<p>Name: Han Solo</p>
<p>DOB: 2010-09-01</p>
<span style="color:#626466;">(This individual has previously worked at: <strong>Scotts</strong> [<i>Wilson and Son</i>] )</span>
<br />

Отображено:

Имя: Фрэнк Спенсер

Дата рождения: 2010-09-01

(Этот человек ранее работал в: Скоттс [ Уилсон и сын ])

Имя: Хан Соло

ДОБ: 2010-09-01

(Этот человек ранее работал в: Скоттс [ Уилсон иСын ])

0 голосов
/ 09 ноября 2010

Это то, что вы ищете?

<xsl:param name="searchName">Brudebakers</xsl:param>
<xsl:param name="searchName2"></xsl:param>

<xsl:variable name="Uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="Lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>

<xsl:template match="/">
    <xsl:copy>
        <body>
            <xsl:apply-templates select="root/listing/name"/>
        </body>
    </xsl:copy>
</xsl:template>

<xsl:template match="name">
    <xsl:apply-templates select=". |following-sibling::dob" mode="fetch-value"/>
    <xsl:apply-templates
            select="following-sibling::details/businessname[contains(., $searchName)]"
            mode="select-one"/>
</xsl:template>

<xsl:template match="node()[name() = 'businessname' or name() = 'firmname']" mode="select-one">
    <xsl:if test="not(../preceding-sibling::details/node()[(name() = 'businessname' or name() = 'firmname')][contains(., $searchName)])">
        <xsl:choose>
            <xsl:when test="contains(., $searchName)">
                <xsl:apply-templates select="." mode="fetch-value"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="./firmname" mode="select-one"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
</xsl:template>


<xsl:template match="@*|node()" mode="fetch-value">
    <p>
        <xsl:value-of select="."/>
    </p>
</xsl:template>

Выход:

<body>
<p>Frank Spencer</p>
<p>2010-09-01</p>
<p>Brudebakers</p>
<p>Han Solo</p>
<p>2010-09-01</p>
<p>Brudebakers</p>
</body>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...