Frage

I am trying to use a key to group <p> elements based on whether they have a specific processing-instruction as a first preceding sibling, but I am having no luck.

For the following example input, I would like each <p> that has a first preceding-sibling processing-instruction that contains “key” to be grouped with its sibling <p> elements that meet the same criteria. Example input:

<root>
    <p>not in key</p>
    <?samplePI key?>
    <p>start of key; </p>
    <?samplePI key?>
    <p>in key 1; </p>
    <?samplePI key?>
    <p>in key 2; </p>
    <p>Not in key</p>
    <?samplePI key?>
    <p>start of new key; </p>
    <?samplePI key?>
    <p>in new key 3;</p>
</root>

Example output:

<root>
    <p>not in key</p>
    <p>start of key; in key 1; in key 2;</p>
    <p>Not in key</p>
    <p>start of new key; in new key 3;</p>
</root>

An example of what I've got:

<xsl:template match="root">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>

    <xsl:template match="p">
        <xsl:choose>
            <xsl:when test="preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]][preceding-sibling::p[1][(preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]])]]">

            </xsl:when>
            <xsl:when test="preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]][preceding-sibling::p[1][not(preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]])]]">
                <p><xsl:value-of select="text()"/>
                    <xsl:apply-templates select="key('nodes', generate-id())" mode="groupedParas"/>
                </p>

            </xsl:when>
            <xsl:otherwise>
                <p><xsl:apply-templates/></p>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="p" mode="groupedParas">
        <xsl:apply-templates/>
    </xsl:template>


    <xsl:key name="nodes" match="node()[(self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]])]" use="generate-id((preceding-sibling::p)[last()])"/>

    <xsl:template match="text()">
        <xsl:value-of select="."/>
    </xsl:template>   

Note that I need the help with getting the correct key syntax, as opposed to generating the desired structure. I need to use XSLT 1.0 for this. Any help appreciated.

War es hilfreich?

Lösung

With XSLT 2.0 it looks manageable

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
                version="2.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:template match="root">
  <xsl:copy>
    <xsl:for-each-group select="*" group-adjacent="boolean(preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]])">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <p>
            <xsl:apply-templates select="current-group()/node()"/>
          </p>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

With XSLT 1.0 my usual approach is sibling recursion but it needs nastily long and convoluted match patterns:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
                version="1.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="processing-instruction()[contains(., 'key')]"/>

<xsl:template match="p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                       and preceding-sibling::node()[2][not(self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]])]]">
  <p>
    <xsl:apply-templates select="." mode="collect"/>
  </p>
</xsl:template>

<xsl:template match="p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                       and preceding-sibling::node()[2][self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]]]]"/>

<xsl:template match="p" mode="collect">
  <xsl:apply-templates/>
  <xsl:apply-templates select="following-sibling::node()[2][self::p and preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]]" mode="collect"/>
</xsl:template>

</xsl:stylesheet>

And finally, as you seem to want to use a key, a variation of the sibling recursion shown above which uses a key to identity a group of p elements, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
                version="1.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="collect"

         match="p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                       and preceding-sibling::node()[2][self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]]]]"
         use="generate-id(preceding-sibling::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                                               and not(preceding-sibling::node()[2][self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]]])][1])"/>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="processing-instruction()[contains(., 'key')]"/>

<xsl:template match="p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                       and preceding-sibling::node()[2][not(self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]])]]">
  <p>
    <xsl:apply-templates select="./node() | key('collect', generate-id())/node()"/>
  </p>
</xsl:template>

<xsl:template match="p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]
                       and preceding-sibling::node()[2][self::p[preceding-sibling::node()[1][self::processing-instruction()[contains(., 'key')]]]]]"/>

</xsl:stylesheet>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top