<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet
  xmlns:my='http://example.org/xxx'
  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  xmlns:xhtml2='http://www.w3.org/foo/bar'
  xmlns:x='file://attributes.to.be.stripped.in.second.pass'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:axsl='file://namespace.alias'
  version='2.0'
  xmlns:a='file://attributes.to.be.stripped.in.first.pass'
>
  <xsl:output
    method='xml'
    indent='yes'
   />
  <xsl:template
    match='/*'
  >
    <rules>
      <xsl:for-each
        select='/*/subject'
      >
        <xsl:variable
          name='subj'
          select='.'
         />
        <xsl:text>&#10;</xsl:text>
        <xsl:comment>
          Subject from Para
          <xsl:value-of
            select='@x:para'
           />
        </xsl:comment>
        <xsl:for-each
          select='/*/predicate'
        >
          <xsl:variable
            name='pred'
            select='.'
           />
          <xsl:text>&#10;</xsl:text>
          <xsl:comment>
            Predicate from Para
            <xsl:value-of
              select='@x:para'
             />
          </xsl:comment>
          <xsl:for-each
            select='/*/object[@a:object=$pred/@a:object]'
          >
            <xsl:variable
              name='obj'
              select='.'
             />
            <xsl:text>&#10;</xsl:text>
            <xsl:comment>
              Object from Para
              <xsl:value-of
                select='@x:para'
               />
            </xsl:comment>
            <match
              select='xhtml2:*{$subj/@a:match}{$pred/@a:match}{$obj/@a:match}'
            >
              <xsl:if
                test='$pred[not(@a:reversed)]'
              >
                <subject>
                  <xsl:apply-templates
                    select='$subj/@*'
                    mode='normal'
                   />
                </subject>
                <predicate>
                  <xsl:apply-templates
                    select='$pred/@*'
                    mode='normal'
                   />
                </predicate>
                <object>
                  <xsl:apply-templates
                    select='$obj/@*'
                    mode='normal'
                   />
                </object>
              </xsl:if>
              <xsl:if
                test='$pred[@a:reversed]'
              >
                <subject>
                  <xsl:apply-templates
                    select='$obj/@*'
                    mode='reversed'
                   />
                </subject>
                <predicate>
                  <xsl:apply-templates
                    select='$pred/@*'
                    mode='reversed'
                   />
                </predicate>
                <object>
                  <xsl:apply-templates
                    select='$subj/@*'
                    mode='reversed'
                   />
                </object>
              </xsl:if>
            </match>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </rules>
  </xsl:template>
  <xsl:template
    mode='normal'
    match='@a:*'
    priority='2'
  >
<!-- discard --></xsl:template>
  <xsl:template
    mode='normal'
    match='@*'
  >
    <xsl:copy />
  </xsl:template>
  <xsl:template
    mode='reversed'
    match='@a:*'
    priority='2'
  >
<!-- discard --></xsl:template>
  <xsl:template
    mode='reversed'
    match='@rdf:about'
    priority='2'
  >
    <xsl:attribute
      name='rdf:resource'
      select='.'
     />
  </xsl:template>
  <xsl:template
    mode='reversed'
    match='@rdf:resource'
    priority='2'
  >
    <xsl:attribute
      name='rdf:about'
      select='.'
     />
  </xsl:template>
  <xsl:template
    mode='reversed'
    match='@*'
  >
    <xsl:copy />
  </xsl:template>
</xsl:stylesheet>

