在 XSLT 中查找 contains() 时停止重复

发布于 2024-10-01 12:21:38 字数 5834 浏览 3 评论 0原文

好吧,我在试图让它发挥作用时已经完全陷入了心理障碍,并且想知道是否其他人能够解决这个问题。我有以下 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>

Okay, I have reached a complete mental block on trying to get this to work and was wondering if anyone else might be able to get their head around it. I have the following structure to 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>

With the following 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>

What I want is to get a list of the firms which contain the search term. But what I get is:

  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)

The xslt should look at both firmname and businessname and then if it finds a match with the contains it should print out only one reference. But I can't seem to get it to work. Has anyone got any solutions/suggestions? The ideal outcome is.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情归归情 2024-10-08 12:21:38

此样式表:

<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: </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 />

渲染:

姓名:Frank Spencer

出生日期:2010-09-01

(此人之前曾在: Scotts [Wilson and Son] 工作)

姓名:Han Solo

出生日期:2010-09-01

(此人之前曾在: Scotts [Wilson and Son] 工作)

This stylesheet:

<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: </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>

Output:

<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 />

Rendered:

Name: Frank Spencer

DOB: 2010-09-01

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

Name: Han Solo

DOB: 2010-09-01

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

装纯掩盖桑 2024-10-08 12:21:38

这是您要找的吗?

<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>

Is it what you looking for?

<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>

Output:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文