xsl-list
[Top] [All Lists]

RE: [xsl] Need help OR'ing in XPATH.

2006-03-23 04:03:01
What would you do if the xml was..

<chapter>
        <owner>aaa <!-- second-->bbb
        <id>222</id>
     </owner>
     </chapter>

and you dont want the value of id to be in the output???

Firstly, I would try to get the design of the XML changed. It's not a good
idea to use mixed content in this way. Mixed content is best used for
marking up narrative text where it makes sense to ignore markup if you are
only interested in the content, for example it's quite acceptable to say

<xsl:if test="contains(title, 'H2SO4')/>

where the title element might be

<title>The properties of H<sub>2</sub>SO<sub>4</sub></title>

If I come across badly-designed XML, my usual approach is to use a two-phase
transformation in which phase 1 cleans up the XML and phase 2 does the
transformation proper. The advantage of this is that phase 1 is reusable, it
can be used unchanged regardless what transformation you want to do in phase
2. In this case the "correct" markup is probably something like

<owner>
  <name>aaa <!-- second-->bbb</name>
  <id>222</id>
</owner>

and the transformation to this form can be achieved in XSLT 2.0 using
something like:

<xsl:template match="owner">
  <xsl:for-each-group select="*" group-adjacent="self::text() or
self::comment()">
    <xsl:choose>
      <xsl:when test="current-grouping-key()">
         <name><xsl:copy-of select="current-group()"/></name>
      </xsl:when>
      <xsl:otherwise>
         <xsl:copy-of select="current-group()"/>
      </xsl:otherwise>
    </
  </
</

(you might also have to do something about whitespace-only text nodes,
depending on whether they need to be retained)

Michael Kay
http://www.saxonica.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>
--~--