[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Insert character for missing nodes


Subject: Re: [xsl] Insert character for missing nodes
From: ac <ac@xxxxxxxxxxxxx>
Date: Fri, 31 Oct 2008 00:26:32 -0400

Hi,

The other solutions look fine but if the elements in business can vary (e.g. applying to various sources), in xslt2
(for xslt1, replace "eq" with "=", "neq" with "!=" and "distinct-values" by the equivalent extension function for your processor),
how about something like


<xsl:variable name="src">
<business>
<name>Hot Dog Stand</name>
<address>123 Main St</address>
<url>http://www.hotdogstand.com</url>
</business>
<business>
<name>Joe's Pizza</name>
<address>213 Pine St</address>
</business>
</xsl:variable>
<xsl:variable name="fields" select="distinct-values($src//business/*/name()[not(. eq 'review')])"/>


   <xsl:template match="business">
     <xsl:variable name="bus" select="."/>
     <xsl:for-each select="$fields">
      <xsl:value-of select="$bus/*[name() eq current()]"/>
      <xsl:if test="position() neq last()">
       <xsl:value-of select="'|'"/>
      </xsl:if>
      </xsl:for-each>
     <xsl:text>&#10;</xsl:text>
   </xsl:template>

ac



Hello,

I am converting XML files to pipe-delimited text files using xsltproc
and xslt version 1. The problem I'm having is that in my XML file,
every possible child node is not present under every parent node.
Here's an example:

<business>
  <name>Hot Dog Stand</name>
  <address>123 Main St</address>
  <url>http://www.hotdogstand.com</url>
</business>
<business>
  <name>Joe's Pizza</name>
  <address>213 Pine St</address>
</business>

Because Joe's Pizza is missing the url node, I don't get a pipe, which
throws off my columns. Is there a way to test for all possible nodes,
and where any are missing, to insert a character?

Here's what I've got so far in my xsl file (I'm excluding the reviews
node because I'm creating separate flat files for the data in that
node):

<xsl:template match="business">
  <xsl:for-each select="*[not(self::reviews)]">
   <xsl:value-of select="."/>
   <xsl:if test="position() != last()">
    <xsl:value-of select="'|'"/>
   </xsl:if>
   </xsl:for-each>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

Thanks,
Jesse


Current Thread
Keywords