Frage

As suggested in here, it seems to be possible to include <xsl:analyze-string> in a <xsl:non-matching-substring>. In my case, I have two regex 'AND facet_(.*?):\(("?.*?"?)\)' and 'AND NOT facet_(.*?):\(("?.*?"?)\)', which are used to construct two different Solr faceting requests to be consumed by my web application, the latter being by exclusion (AND NOT).

This is my XML:

   <facets>
      <facet name="domain">
        <facetEntry count="8007">domain1.co.uk</facetEntry>
        <facetEntry count="6497">domain2.co.uk</facetEntry>
        <facetEntry count="6180">domain3.co.uk</facetEntry>            
      </facet>
   <facets>

Whereas, this is the template I am using:

<xsl:template name="facets">
                <xsl:param name="q" />
                <xsl:analyze-string select="$q" regex='AND facet_(.*?):\(("?.*?"?)\)'>
                        <xsl:matching-substring>
                        <xsl:choose>
                                <xsl:when test="regex-group(1) = 'example1'">
                                    <facet name="domain"><xsl:value-of select="regex-group(2)" /></facet>
                                </xsl:when>
                                <xsl:when test="regex-group(1) = 'example2'">
                                    <facet name="content_type_norm"><xsl:value-of select="regex-group(2)" /></facet>
                                </xsl:when>
                                <xsl:when test="regex-group(1) = 'example2'">
                                    <facet name="sentiment"><xsl:value-of select="regex-group(2)" /></facet>
                                </xsl:when>
                        </xsl:choose>
                        </xsl:matching-substring>
                       <xsl:non-matching-substring></xsl:non-matching-substring>
                </xsl:analyze-string>
    </xsl:template>

I have made several attempts to include an extra <xsl:analyze-string> inside , but I am always returned with a syntax error. For example:

    <xsl:non-matching-substring>
                           <xsl:analyze-string select="$q" regex='AND NOT facet_(.*?):\(("?.*?"?)\)'>
<xsl:choose>
                                    <xsl:when test="regex-group(1) = 'example1'">
                                        <facet name="domain"><xsl:value-of select="regex-group(2)" /></facet>
                                    </xsl:when>
                                    <xsl:when test="regex-group(1) = 'example2'">
                                        <facet name="content_type_norm"><xsl:value-of select="regex-group(2)" /></facet>
                                    </xsl:when>
                                    <xsl:when test="regex-group(1) = 'example2'">
                                        <facet name="sentiment"><xsl:value-of select="regex-group(2)" /></facet>
                                    </xsl:when>
                            </xsl:choose>
                         </xsl:non-matching-substring>
                    </xsl:analyze-string>

In that case I am returned with the following error:

The element type "xsl:analyze-string" must be terminated by the matching end-tag "".

I am rather stuck and I am not sure whether this is really allowed in XSLT. Can you help?

Thanks indeed,

I.

War es hilfreich?

Lösung

You can nest a second xsl:analyze-string into the xsl:non-matching-substring if you want but of course the content of any xsl:analyze-string has to follow the rules

The content of the xsl:analyze-string instruction must take one of the following forms:

A single xsl:matching-substring instruction, followed by zero or more xsl:fallback instructions

A single xsl:non-matching-substring instruction, followed by zero or more xsl:fallback instructions

A single xsl:matching-substring instruction, followed by a single xsl:non-matching-substring instruction, followed by zero or more

xsl:fallback instructions

[ERR XTSE1130] It is a static error if the xsl:analyze-string instruction contains neither an xsl:matching-substring nor an xsl:non-matching-substring element.

So don't put an xsl:choose inside of it directly, rather xsl:matching-substring and/or xsl:non-matching-substring, which can then take any sequence constructor if needed, which allows xsl:choose of course.

Here is a complete example that is well-formed XML and syntactically correct XSLT:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:template name="facets">
  <xsl:param name="q" />
  <xsl:analyze-string select="$q" regex='AND facet_(.*?):\(("?.*?"?)\)'>
    <xsl:matching-substring>
      <xsl:choose>
        <xsl:when test="regex-group(1) = 'example1'">
          <facet name="domain"><xsl:value-of select="regex-group(2)" /></facet>
        </xsl:when>
        <xsl:when test="regex-group(1) = 'example2'">
          <facet name="content_type_norm"><xsl:value-of select="regex-group(2)" /></facet>
        </xsl:when>
        <xsl:when test="regex-group(1) = 'example2'">
          <facet name="sentiment"><xsl:value-of select="regex-group(2)" /></facet>
        </xsl:when>
      </xsl:choose>
    </xsl:matching-substring>
    <xsl:non-matching-substring>                                                    
      <xsl:analyze-string select="$q" regex='AND NOT facet_(.*?):\(("?.*?"?)\)'>
        <xsl:matching-substring>
          <xsl:choose>
            <xsl:when test="regex-group(1) = 'example1'">
              <facet name="domain"><xsl:value-of select="regex-group(2)" /></facet>
            </xsl:when>
            <xsl:when test="regex-group(1) = 'example2'">
              <facet name="content_type_norm"><xsl:value-of select="regex-group(2)" /></facet>
            </xsl:when>
            <xsl:when test="regex-group(1) = 'example2'">
              <facet name="sentiment"><xsl:value-of select="regex-group(2)" /></facet>
            </xsl:when>
          </xsl:choose>
        </xsl:matching-substring>
      </xsl:analyze-string>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

Try to use an XML editor or editor plugin, it helps getting the proper nesting, and tag completion should avoid errors with typos like </xsl:matching-sustring>.

Andere Tipps

Before you can write XSLT, you have to learn how to write XML. A start tag named xsl:analyze-string can't be matched by an end tag named xsl:choose.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top