Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 7th, 2004, 03:48 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML, checking attributes

Say i have the following:
<?xml version="1.0" encoding="utf-8"?>
<rootelement>
<level1 id="id_1" a="1" b="1" c="1" d="1">
  <level2 id="id_2" a="1" b="0" c="0" d="0">
    <contents>Some contents/contents>
  </level2>
  <level2 id="id_3" a="0" b="0" c="0" d="0">
    <contents>Some contents</contents>
  </level2>
</level1>
<level1 id="id_4" a="1" b="1" c="1" d="1">
  <level2 id="id_5" a="1" b="1" c="1" d="1">
    <level3 id="id_6" a="1" b="1" c="1" d="1">
      <contents>Some contents/contents>
    </level3>
    <level3 id="id_7" a="0" b="0" c="0" d="0">
      <contents>Some contents</contents>
    </level3>
  </level2>
</level1>
</rootelement>

Now i want to end up with an XML-document with only those elements which attribute 'a' equals 1, so:

<?xml version="1.0" encoding="utf-8"?>
<rootelement>
<level1 id="id_1">
  <level2 id="id_2">
    <contents>Some contents/contents>
  </level2>
</level1>
<level1 id="id_4">
  <level2 id="id_5">
    <level3 id="id_6">
      <contents>Some contents/contents>
    </level3>
  </level2>
</level1>
</rootelement>

I have made several attempts, but i always end up with either too much or too less information in the resultdocument.
Note: I don't know how much 'levels' the document has!

Any help is greatly appreciated!
Regards, Raoul.


 
Old December 7th, 2004, 05:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No-one can tell you where you're going wrong unless you show your code!



Michael Kay
http://www.saxonica.com/
 
Old December 8th, 2004, 04:37 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All right, here is my stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" encoding="utf-8" />

  <xsl:template match="/">
    <xsl:element name="rootdocument">
      <xsl:apply-templates select="*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="name() = 'contents'">
        <xsl:if test="../@a = '1'">
          <xsl:copy-of select="." />
        </xsl:if>
      </xsl:when>
      <xsl:when test="starts-with(name(), 'level')">
        <xsl:if test="@a = '1'">
          <xsl:element name="{name()}" use-attribute-sets="Item">

          </xsl:element>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise />
    </xsl:choose>
    <xsl:apply-templates />
  </xsl:template>

  <xsl:attribute-set name="Item">
    <xsl:attribute name="id">
      <xsl:value-of select="@id" />
    </xsl:attribute>
  </xsl:attribute-set>

</xsl:stylesheet>

 
Old December 8th, 2004, 08:17 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

Quick fix:
Uncomment the inside your <xsl:when test="starts-with(name(), 'level')"> block, otherwise the elements won't nest at all.

Move the <xsl:apply-templates /> after the <xsl:choice>, inside, to the <xsl:otherwise> I suppose.
---
Smarter more durable fix: when you call apply-templates, only select the ones you want, eg.
<xsl:apply-templates select="*[(@a = '1') or not (@a)]/>
 
Old December 9th, 2004, 10:15 AM
Registered User
 
Join Date: Jun 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much, it works just perfectly.







Similar Threads
Thread Thread Starter Forum Replies Last Post
copying xml attributes golddog XSLT 1 September 12th, 2007 01:05 PM
Retrieving xml attributes christianhau Classic ASP XML 0 July 19th, 2007 06:25 AM
Using xml elements as html attributes chipmaster XSLT 4 June 26th, 2007 10:22 AM
Looping through attributes in an XML document francislang XML 4 November 17th, 2004 11:02 AM
XML attributes miguel.ossa C# 2 February 9th, 2004 07:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.