XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
RE: [xml-dev] XSLT stylesheet looping through the same node (recursion?)

It looks to me as if you would be better off using pattern matching
 

<xsl:template match="/">

  <area>

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

  </area>

</xsl:template>

 
<xsl:template match="ns:p[ns:strong='Interest:']">
  <xsl:for-each select="text()[normalize-space() != '']">
    <interest><xsl:value-of select="normalize-space()"/></interest>
  </xsl:for-each>
</xsl:template>
 
<xsl:template match="ns:p"/>
 
Michael Kay
http://www.saxonica.com/


From: Jack Bush [mailto:netbeansfan@yahoo.com.au]
Sent: 26 January 2009 13:31
To: xml-dev@lists.xml.org
Subject: [xml-dev] XSLT stylesheet looping through the same node (recursion?)

Hi All,

 

I would like to get all the content/text in <p> node from the XML document where <p> has a child node <strong>Interest:<strong>. Below is the listing of both XML document and XSLT stylesheet:

 

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

  <!DOCTYPE html (View Source for full doctype...)>

- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">

- <head>

- <body

   ........

- ><div id="container">

+ <div id="header">

  <div id="postmark" />

- <div id="content">

+ <div id="as1">

  .......

  <h1>Employee Detail</h1>

- <p>

  <strong>Interest:</strong>

  <br  />

  Tennis

  <br  />

  Movie

<br  />

Swimming

<br  />

.....

 

 

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:ns="http://www.w3.org/1999/xhtml"

exclude-result-prefixes="ns">

 

 

<xsl:template match="/">

<area>

- <xsl:for-each select="/ns:html/ns:body/ns:div[@id='content']/ns:p/ns:strong">

- <xsl:if test="contains(.,'Interest:')">

- <xsl:choose>

- <xsl:when test="contains(.,'Interest:')">

- <interest>

  <xsl:value-of select="../text()" />

  </interest>

  </xsl:when>

- <xsl:otherwise>

  <interest>Unknown</interest>

  </xsl:otherwise>

  </xsl:choose>

</xsl:if>

</xsl:template>

 

</xsl:stylesheet>

 

 

The transformation process produces the following output:

 

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

<html xmlns="http://www.w3.org/1999/xhtml">

  <title>Employee Detail</title>

  <body>

      <h1>Employee Detail</h1>

      <interest>Tennis</interest>

  </body>

</title>

</html>  

 

One possible solution is by using recursion to go through the list of values on nodes but I am cannot think of how this could be done. Perhaps my difficulty is that I don’t know how to navigate down the list of interest of the same node using xsl command. Most of the Googled articles consisted of searching for the same type of element a cross multiple nodes, where as I am looking all the values (same type anyway) in a single node.

 

Below is the JDOM XPath solution to get the same list of values:

 

XPath interestxpath = XPath.newInstance("/ns:html/ns:body/ns:div[@id='content']/ns:p[10]/node()");

interestxpath.addNamespace("ns", "http://www.w3.org/1999/xhtml");

java.util.List interest_list =                       interestxpath.selectNodes(employeejdomDocument);

Iterator interest_iterator = interest_list.iterator();

while (interest_iterator..hasNext())

{

   interest_value = ((org.jdom.Content)interest_iterator.next()).getValue();

……

 

I am new to XSLT stylesheet transformation and can do with some guidances.

 

Many thanks again,

 

Jack



Stay connected to the people that matter most with a smarter inbox. Take a look.


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS