aarondev
aarondev

Reputation: 95

Select text from child node and omit text node

I'm basically looking for the opposite of this

An example of the XML I'm dealing with:

<profiledesc>
   <creation>
       Finding Aid encoded by Some Guy, <date normal="2011-09-21">21 September 2011</date>
   </creation>
   <langusage encodinganalog="546">
      Finding aid written in
      <language langcode="eng" scriptcode="latn" encodinganalog="041">
         English   
      </language>
   </langusage>
</profiledesc>

An example of the XSLT I'm writting (only the relevant parts):

<xsl:template priority="3" match="descgrp|eadheader|filedesc|titlestmt|profiledesc|archdesc|langusage|did">
   <xsl:apply-templates select="./child::node()"/>
</xsl:template>

<xsl:template priority="2" match="language">
   <atom name="EADLanguageOfFindingAid" type="text" size="short">
      <xsl:value-of select="."/>
   </atom>
   <atom name="EADLanguageCodeOfFindingAid" type="text" size="short">
       <xsl:value-of select="normalize-space(@langcode)"/>
   </atom>
</xsl:template>

... Other templates, for nodes like 'creation' ....

An example of the (bad) output I'm getting:

... Some other tags ...
<atom name="EADCreation" type="text" size="short">Finding Aid encoded by Some Guy, 21 September 2011</atom>
Finding aid written in
<atom name="EADLanguageOfFindingAid" type="text" size="short"> English </atom>
<atom name="EADLanguageCodeOfFindingAid" type="text" size="short">eng</atom>
... Some other tags ...

An example of the (good) output I want:

... Some other tags ...
<atom name="EADCreation" type="text" size="short">Finding Aid encoded by Some Guy, 21 September 2011</atom>
<atom name="EADLanguageOfFindingAid" type="text" size="short"> English </atom>
<atom name="EADLanguageCodeOfFindingAid" type="text" size="short">eng</atom>
... Some other tags ...

Note that in the second output the "Finding aid written in" line is missing.

So, as you can see, I designed templates to output just the "language" portion of the "langusage" tag, but the whole tag, including the "Finding aid written in" text node, is being output instead. I can't be sure that the text node will exist or that it will be first (or last, or in any particular position). I also can't be sure that there will only be one text node or one child node. So, I can't make use of any solution that relies on simply choosing the "[xth]" node (child or text).

I'd appreciate any advice at this point, even some keywords that would help me find the solution via Google (I've had no luck there so far).

Upvotes: 0

Views: 421

Answers (1)

Wayne
Wayne

Reputation: 60424

It sounds like you want to select only the child elements of langusage instead of all child nodes (of any type, which is what node() selects (excluding attribute nodes and the root node)).

For example, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="langusage">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="language">
        <atom name="EADLanguageOfFindingAid" type="text" size="short">
            <xsl:value-of select="normalize-space()"/>
        </atom>
        <atom name="EADLanguageCodeOfFindingAid" type="text" size="short">
            <xsl:value-of select="normalize-space(@langcode)"/>
        </atom>
    </xsl:template>
</xsl:stylesheet>

Applied to this simplified input:

<profiledesc>
   <langusage encodinganalog="546">
      Finding aid written in
      <language langcode="eng" scriptcode="latn" encodinganalog="041">
         English   
      </language>
      <language langcode="esp" scriptcode="latn" encodinganalog="042">
         Spanish   
      </language>
   </langusage>
</profiledesc>

Produces the following output:

<atom name="EADLanguageOfFindingAid" type="text" size="short">English</atom>
<atom name="EADLanguageCodeOfFindingAid" type="text" size="short">eng</atom>
<atom name="EADLanguageOfFindingAid" type="text" size="short">Spanish</atom>
<atom name="EADLanguageCodeOfFindingAid" type="text" size="short">esp</atom>

The unwanted text -- Finding aid written in -- does not appear in the output.

Note that the * in:

<xsl:apply-templates select="*"/>

...is just a shorter way to say child::*. Both select all element children of the current node.

Upvotes: 1

Related Questions