xsl-list
[Top] [All Lists]

Re: [xsl] Unable to select wanted nodes

2011-10-06 18:10:02
Sorry, folks, responded to the wrong message.

Ken had pointed out to me off-list that the use of "name(@*)" will
throw an error in XSLT 2.0 if there is more than one attribute on the
element.  While the input data, in this case, appears to always have
only one attribute, it's not clear whether or not this is ensured by a
schema.  The error thrown in the case where there is more than one
attribute might be seen as a type of validation check, but probably
not a very good or helpful one.  Point is, this is a somewhat
dangerous construct to use without some checks to ensure there aren't
multiple attributes.

Thanks, Ken, for the tip, and my apologies again for the misdirected
reply (though I still would like to know about the new course!).

-Brandon :)


On Thu, Oct 6, 2011 at 7:03 PM, Brandon Ibach
<brandon(_dot_)ibach(_at_)single-sourcing(_dot_)com> wrote:
Hi, Ken...

This is a good point.  I wasn't aware that this would throw an error
in 2.0, but for what it's worth, that might really be desired
behavior, anyway.  His model seems to be that the element in question
should only ever have one attribute.  I don't know if he's using any
kind of schema to enforce that, but if he isn't, then the error thrown
by the transform would essentially be a (admittedly clunky, likely
with a not-very-helpful error message) form of validation check.

In any case, it might be worth explaining the detail, for the archives.

I've been meaning to ask, what's the new course you mentioned working
on recently?

-Brandon :)


On Thu, Oct 6, 2011 at 4:02 PM, Mark <mark(_at_)knihtisk(_dot_)org> wrote:
For the below <Input> file, I want the <DesiredOutput> file but so far I
have only been able to write code that produces <MyOutput>.

In <MyOutPut>:
(1) The mode="stamp" templates produce satisfactory <StampPage> elements.
These templates and their output were included only for completeness and may
be ignored.

(2) The mode="formats" templates produce the <FormatPage> and its child
<StampButtons> properly, but I cannot puzzle out how to produce the children
of <FormatButtons>.  I am not even sure that what I have done so far is the
best way to do this.

Side note: the redundant attribute in the <FormatPage> element appears
solely as debugging aid.

Help graciously accepted,
Mark

<Input>
  <Set>
    <Stamp>
      <CatNumbers pofis-number="105"/>
      <Formats souvenir-sheet="105"/>
      <Formats se-tenant="105"/>
    </Stamp>
    <Stamp>
      <CatNumbers pofis-number="106"/>
      <Formats souvenir-sheet="105"/>
      <Formats se-tenant="105"/>
    </Stamp>
    <Stamp>
      <CatNumbers pofis-number="107"/>
      <Formats souvenir-sheet="105"/>
      <Formats se-tenant="107"/>
    </Stamp>
    <Stamp>
      <CatNumbers pofis-number="108"/>
      <Formats souvenir-sheet="105"/>
      <Formats se-tenant="107"/>
    </Stamp>
  </Set>
 </Input>
----------------
<DesiredOutput>
  <StampPage>
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats state="active" se-tenant="105"/>
    </FormatButtons>
    <StampButtons>
      <Stamp state="depressed" pofis-number="105"/>
    </StampButtons>
  </StampPage>
  <StampPage>
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats state="active" se-tenant="105"/>
    </FormatButtons>
    <StampButtons>
      <Stamp state="depressed" pofis-number="106"/>
    </StampButtons>
  </StampPage>
  <StampPage >
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats state="active" se-tenant="107"/>
    </FormatButtons>
    <StampButtons>
      <Stamp state="depressed" pofis-number="107"/>
    </StampButtons>
  </StampPage>
  <StampPage>
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats state="active" se-tenant="107"/>
    </FormatButtons>
    <StampButtons>
      <Stamp state="depressed" pofis-number="108"/>
    </StampButtons>
  </StampPage>
  <FormatPage souvenir-sheet="105">
    <FormatButtons>
      <Formats state="depressed" souvenir-sheet="105"/>
      <Formats label="1" state="active" se-tenant="105"/>
      <Formats label="2" state="active" se-tenant="107"/>
    </FormatButtons>
    <StampButtons>
      <Stamp label="1" state="active" pofis-number="105"/>
      <Stamp label="2" state="active" pofis-number="106"/>
      <Stamp label="3" state="active" pofis-number="107"/>
      <Stamp label="4" state="active" pofis-number="108"/>
    </StampButtons>
  </FormatPage>
  <FormatPage se-tenant="105">
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats label="1" state="depressed" se-tenant="105"/>
      <Formats label="2" state="active" se-tenant="107"/>
    </FormatButtons>
    <StampButtons>
      <Stamp label="1" state="active" pofis-number="105"/>
      <Stamp label="2" state="active" pofis-number="106"/>
    </StampButtons>
  </FormatPage>
  <FormatPage se-tenant="107">
    <FormatButtons>
      <Formats state="active" souvenir-sheet="105"/>
      <Formats label="1" state="active" se-tenant="105"/>
      <Formats label="2" state="depressed" se-tenant="107"/>
    </FormatButtons>
    <StampButtons>
      <Stamp label="1" state="active" pofis-number="107"/>
      <Stamp label="2" state="active" pofis-number="108"/>
    </StampButtons>
  </FormatPage>
 </DesiredOutput>

-------------------
<MyOutput>
 <StampPage>
  <FormatButtons>
    <Formats state="active" souvenir-sheet="105"/>
    <Formats state="active" se-tenant="105"/>
  </FormatButtons>
  <StampButtons>
    <Stamp state="depressed" pofis-number="105"/>
  </StampButtons>
 </StampPage>
 <StampPage>
  <FormatButtons>
    <Formats state="active" souvenir-sheet="105"/>
    <Formats state="active" se-tenant="105"/>
  </FormatButtons>
  <StampButtons>
    <Stamp state="depressed" pofis-number="106"/>
  </StampButtons>
 </StampPage>
 <StampPage>
  <FormatButtons>
    <Formats state="active" souvenir-sheet="105"/>
    <Formats state="active" se-tenant="107"/>
  </FormatButtons>
  <StampButtons>
    <Stamp state="depressed" pofis-number="107"/>
  </StampButtons>
 </StampPage>
 <StampPage>
  <FormatButtons>
    <Formats state="active" souvenir-sheet="105"/>
    <Formats state="active" se-tenant="107"/>
  </FormatButtons>
  <StampButtons>
    <Stamp state="depressed" pofis-number="108"/>
  </StampButtons>
 </StampPage>
 <FormatPage souvenir-sheet="105">
  <FormatButtons>
    <!-- My problem -->
  </FormatButtons>
  <StampButtons>
    <Stamp label="1" state="active" pofis-number="105"/>
    <Stamp label="2" state="active" pofis-number="106"/>
    <Stamp label="3" state="active" pofis-number="107"/>
    <Stamp label="4" state="active" pofis-number="108"/>
  </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="105">
  <FormatButtons>
    <!-- My problem -->
  </FormatButtons>
  <StampButtons>
    <Stamp label="1" state="active" pofis-number="105"/>
    <Stamp label="2" state="active" pofis-number="106"/>
  </StampButtons>
 </FormatPage>
 <FormatPage se-tenant="107">
  <FormatButtons>
    <!-- My problem -->
  </FormatButtons>
  <StampButtons>
    <Stamp label="1" state="active" pofis-number="107"/>
    <Stamp label="2" state="active" pofis-number="108"/>
  </StampButtons>
 </FormatPage>
</MyOutput>

------------------------
Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"; exclude-result-prefixes="xs
xd"
 version="2.0">

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

 <xsl:template match="Set">
  <xsl:element name="Output">
    <xsl:apply-templates mode="stamp"/>
    <xsl:apply-templates mode="formats"/>
  </xsl:element>
 </xsl:template>

 <!-- Create a StampPage for every Stamp -->
 <xsl:template match="Stamp" mode="stamp">
  <xsl:element name="StampPage">
    <xsl:element name="FormatButtons">
      <xsl:apply-templates mode="stamp"/>
    </xsl:element>
    <xsl:element name="StampButtons">
      <xsl:element name="Stamp">
        <xsl:attribute name="state" select="'depressed'"/>
        <xsl:attribute name="pofis-number"
select="CatNumbers/@pofis-number"/>
      </xsl:element>
    </xsl:element>
  </xsl:element>
 </xsl:template>

 <xsl:template match="Stamp" mode="formats">
  <xsl:apply-templates mode="formats"/>
 </xsl:template>

 <xsl:template match="Formats" mode="stamp">
  <xsl:element name="Formats">
    <xsl:attribute name="state" select="'active'"/>
    <xsl:copy-of select="@*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="Formats" mode="formats">
  <xsl:if test="@* eq ../CatNumbers/@pofis-number">
    <xsl:element name="FormatPage">
      <xsl:copy-of select="@*"/>
      <xsl:variable name="format" select="name(@*)"/>
      <xsl:variable name="number" select="@*"/>
      <xsl:element name="FormatButtons">
       <!-- I do not know how to select the correct components -->
      </xsl:element>
      <xsl:element name="StampButtons">
        <xsl:choose>
          <xsl:when test="count(../../Stamp/Formats/@*[name(.)=$format]) eq
1">
            <xsl:element name="Stamp">
              <xsl:attribute name="state" select="'active'"/>
              <xsl:copy-of select="../CatNumbers/@pofis-number"/>
            </xsl:element>
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each-group select="../../Stamp/Formats"
group-by="@*[name(.)=$format]">
              <xsl:for-each select="current-group()">
                <xsl:if test="@* eq $number">
                  <xsl:element name="Stamp">
                    <xsl:attribute name="label" select="position()"/>
                    <xsl:attribute name="state" select="'active'"/>
                    <xsl:copy-of select="../CatNumbers/@pofis-number"/>
                  </xsl:element>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each-group>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:element>
  </xsl:if>
 </xsl:template>

</xsl:stylesheet>


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>