Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the value of Child tags before/after Current Tag

Status
Not open for further replies.

mac7attack

Technical User
Jan 31, 2004
47
0
0
US
Hello all,

Is there any way to test to see if the current child tag is the same as the child tag in the parent before or after the child?

Code:
<Questions>
  <question>
    <category>Outlook</category>
     <ask>Some Question</ask>
  </question>
  <question>
    <category>Outlook</category>
     <ask>Another Question</ask>
  </question>
  <question>
    <category>Explorer</category>
     <ask>LastQuestion</ask>
 </question>
</Question>

So using the above xml file and a xslt file, is there a way when processing the second question to test if category tag is the tag as the one above it? I know there should be a position test first to process the first tag differently.
The test should be positive at the second tag and negative on the third.

Thanks in advanced,
Matt

 
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="question">
    <xsl:copy>
      <xsl:attribute name="test">
        <xsl:choose>
          <xsl:when test="category = following-sibling::node()/category or category = preceding-sibling::node()/category">true</xsl:when>
          <xsl:otherwise>false</xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Looks like good. Thanks John

Just one question,
What does template match="@*|node()" do? It look likes it implenting itself again

Thanks
Matt
 
match="@*|node()" says match any attribute or node, i.e. anything.

The default template in XSL will simply output the text value. The template match="@*|node()" is overriding that and saying output the XML. Its very useful, because when you specify another template (ie match="question") this is more specific and so takes precedence. So you can copy the structure of the orginal document and change a particular node.

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top