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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Qs: putting new elements in one big element in xslt

Status
Not open for further replies.

foohoo

Programmer
Mar 4, 2006
3
GB
Hi,

i am quite new to this so bare with me:) I have a source xml document that looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="trans2.xsl"?>

<people>

<name firstname="John" middlename="James" lastname="Weston"/>

<name firstname="Sally" middlename="Ann" lastname="Jones"/>

<name firstname="David" middlename="Charles" lastname="Smith"/>

<name firstname="Peter" middlename="John" lastname="Patel"/>

<name firstname="Dexter" middlename="James" lastname="Barnes"/>

</people>

very simple...i have created a stylesheet xsl that puts these attributes into elements (firstname, middlename, lastname) and those elements into a element called Employee as shown:

<Employee>
<firstname>....</firstname>
<middlename>...</middlename>
<lastname>...</lastname>
</Employee>

and there are n number of employees. i did that with the xsl:

<xsl:stylesheet version="1.0"
xmlns:xsl="<xsl:eek:utput method="xml" omit-xml-declaration="yes" indent="no"/>


<xsl:template match="people/name">

<Employee>
<xsl:text>
</xsl:text>

<firstname>
<xsl:value-of select="@firstname"/>
</firstname>

<xsl:text>
</xsl:text>

<middlename>
<xsl:value-of select="@middlename"/>
</middlename>

<xsl:text>
</xsl:text>

<lastname>
<xsl:value-of select="@lastname"/>
</lastname>

<xsl:text>
</xsl:text>

</Employee>

</xsl:template>


</xsl:stylesheet>

what i would like to do is have a parent node to Employee called staff so it looks like:

<staff>
<Employee>
.
.
</Employee>
<Employee>
.
.
</Employee>
etc..
</staff>

how do i do that??
(sorry for long post)

foohoo
 
To foohoo. This can be done simply by insert a template matching the root, insert the element staff and apply your template from within the element.
[tt]
<xsl:template match="/">
<xsl:element name="staff">
<xsl:apply-templates match="people/name" />
</xsl:element>
</xsl:template>
[/tt]
 
Hi, again

Thx for that..it works...slightly. It seems to work fine when i view it in mozilla of IE but i tried to use it with saxon by typing

java -jar saxon.jar -a companyA.xml

and i get an error messgae that says:

Attribute match is not allowed on this element
Transformation failed: Failed to compile stylesheet. 1 error detected.

and it points at the new:

<xsl:apply-templates match="people/name" />

but it works fine in mozilla or IE
the new code looks like:

<xsl:stylesheet version="1.0"
xmlns:xsl="<xsl:eek:utput method="xml" omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:element name="staff">
<xsl:apply-templates match="people/name" />
</xsl:element>
</xsl:template>

<xsl:template match="people/name">
<Employee>

<firstname>
<xsl:value-of select="@firstname"/>
</firstname>

<middlename>
<xsl:value-of select="@middlename"/>
</middlename>

<lastname>
<xsl:value-of select="@lastname"/>
</lastname>

</Employee>
</xsl:template>

</xsl:stylesheet>
 
Sorry! That's my hasty mistake. The error message would sure not be limited to saxon, because it is meant to be select! It would error out practically everywhere.

The line should sure be read.
[tt] <xsl:apply-templates [red]select[/red]="people/name" />[/tt]

Sorry for the confusion.
 
More easily extensible would be to do something like this:
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="@*">
    <xsl:element name="{name(.)}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="people">
    <staff>
      <xsl:apply-templates select="@*|node()"/>
    </staff>
  </xsl:template>
  <xsl:template match="name">
    <employee>
      <xsl:apply-templates select="@*|node()"/>
    </employee>
  </xsl:template>
</xsl:stylesheet>

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