RE: [xsl] Copy Child Elements

Subject: RE: [xsl] Copy Child Elements
From: "Wei, Alice J." <ajwei@xxxxxxxxxxx>
Date: Sat, 26 Jan 2008 06:34:14 -0500
Hi, Rick:

   I thought others would on the list would follow up on this message, but
looking over at your XSLT again (I am assuming you are using 1.0 and not
2.0),
  here is one additional problem:

  You have nested <refbody> inside the <xsl:template match="/"> and unless you
really define the immediate child, all you get will be something like

  <?xml version="1.0" encoding="utf-8" ?>
  <definitions />

  To fix this problem, here is the code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
           <xsl:output method="xml" />
        <xsl:template match="/">
            <definitions>
                <xsl:apply-templates />
            </definitions>
        </xsl:template>

        <xsl:template match="refbody">
            <xsl:apply-templates select="section">
                <xsl:sort select="title" />
            </xsl:apply-templates>
        </xsl:template>

        <xsl:template match="section">
            <xsl:for-each select="p[position()&lt;=2]">
                <xsl:if test="position()=1">
                    <dt>
                        <xsl:apply-templates />
                    </dt>
                </xsl:if>
                <xsl:if test="position()=2">
                    <dl>
                        <xsl:apply-templates />
                    </dl>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>

    </xsl:stylesheet>

Your Output:

  <?xml version="1.0" encoding="utf-8" ?>
 <definitions>
  <dt>Author Job Title</dt>
  <dl>Setting that indicates...</dl>
  <dt>Type (of definition)</dt>
  <dl>Type of the performance... Focal- A focal review is the most typical
type...</dl>
  </definitions>

I hope this explains the solution to your problem, and this is the output you
desired it to be.

Hope this helps.

Alice

======================================================
Alice Wei
MIS 2008
School of Library and Information Science
Indiana University Bloomington
ajwei@xxxxxxxxxxx
________________________________________
From: Wei, Alice J.
Sent: Friday, January 25, 2008 10:02 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Copy Child Elements

Hi,

  There is one obvious problem with your XSL here:

  If you are using apply-templates, what it does is that it would
select any of the children of the template root. Use
<xsl:apply-templates select="section"> and and <xsl:apply-templates
select="//section">

  I am sure the others on the list would be able to help you with some
of the other mistakes.

Good luck.

Alice


Quoting Rick Quatro <frameexpert@xxxxxxxxxxxx>:

> Hello,
>
> Here is my input XML:
>
> <?xml version="1.0" encoding="utf-8"?>
> <reference>
>  <refbody>
>    <section>
>      <title>Author Job Title</title>
>      <p id="Field_ShowAuthorJobTitle">Author Job Title</p>
>      <p id="Desc_ShowAuthorJobTitle">Setting that indicates...</p>
>      <p id="AppTo_ShowAuthorJobTitle">Review Definitions, Section Item</p>
>      <p id="Hdr_ShowAuthorJobTitle">Author Settings</p>
>    </section>
>    <section>
>      <title>Type (of definition)</title>
>      <p id="Field_Typeofdefinition">Type (of definition)</p>
>      <p id="Desc_Typeofdefinition">Type of the performance...
>        <ul>
>          <li>
>            <p><b>Focal</b>- A focal review is the most typical type...</p>
>          </li>
>        </ul></p>
>      <p id="AppTo_Typeofdefinition">Review Definitions, Section Item,
> Performance Review</p>
>      <p id="Hdr_Typeofdefinition">Properties</p>
>    </section>
>  </refbody>
> </reference>
>
> Here is my desired output, except for the problem described below the
output.
>
> <?xml version="1.0"?>
> <definitions>
>  <dt>Author Job Title</dt>
>  <dl>Setting that indicates...</dl>
>  <dt>Type (of definition)</dt>
>  <dl>Type of the performance... Focal- A focal review is the most
> typical type...</dl>
> </definitions>
>
> For the <dt> and <dl> elements, I want to include any contents from
> the original content; for example the <ul> element in the second
> <section> element, etc.
>
> Here is my stylesheet:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>  <xsl:output method="xml" />
>  <xsl:template match="/">
>    <definitions>
>      <xsl:apply-templates />
>    </definitions>
>  </xsl:template>
>
>  <xsl:template match="reference">
>    <xsl:apply-templates select="//section">
>      <xsl:sort select="title" />
>    </xsl:apply-templates>
>  </xsl:template>
>
>  <xsl:template match="section">
>    <xsl:for-each select="p[position()&lt;=2]">
>      <xsl:if test="position()=1">
>        <dt>
>          <xsl:apply-templates />
>        </dt>
>      </xsl:if>
>      <xsl:if test="position()=2">
>        <dl>
>          <xsl:apply-templates />
>        </dl>
>      </xsl:if>
>    </xsl:for-each>
>  </xsl:template>
>
> </xsl:stylesheet>
>
> Any help is appreciated.
>
> Rick Quatro
> Carmen Publishing
> 585-659-8267
> www.frameexpert.com
>
>



Alice Wei
MIS 2008
School of Library and Information Science
Indiana University Bloomington
ajwei@xxxxxxxxxxx

Current Thread