?
Solved

XSL to filter XML based on attributes and using conditions

Posted on 2003-02-25
4
Medium Priority
?
256 Views
Last Modified: 2013-11-19
Hello,
I have the following xml/xsd file:
<?xml version="1.0"?>
<xs:schema xmnls:xs="http://www.w3.org/2001/XMLSchema"  targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">
<xs:element name ="note">
    <xs:complexType>
         <xs:sequence>
              <xs:element name = "to" fixed="1000" type ="xs:string"/>
              <xs:element name = "from"  type ="xs:string"/>
              <xs:element name = "heading"  type ="xs:string"/>
              <xs:element name = "body" fixed ="0001" type ="xs:string"/>
         </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

so i'm trying to create a xsl file which i can apply to this xml/xsd file in order to output another (same) xsd/xsl file containing same things which xs:element filtered, following those conditions:

- if the attribute: fixed= "1000" then the xs:element is deleted
- if there is no "fixed" attribute then the xs:element is copied
- if the attribute: fixed ="0001" then the xs:element is copied without copying the attribute:fixed and also there is a string (ex:TARGET) which is concatenated to the xs:element name
(ex: <xs:element name = "body" fixed ="0001" type ="xs:string"/> becomes <xs:element name = "TARGETbody" type ="xs:string"/>



So in this case, the output after the xsl transformation should give:
<?xml version="1.0"?>
<xs:schema xmnls:xs="http://www.w3.org/2001/XMLSchema"  targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">
<xs:element name ="note">
    <xs:complexType>
         <xs:sequence>
              <xs:element name = "from"  type ="xs:string"/>
              <xs:element name = "heading"  type ="xs:string"/>
              <xs:element name = "TARGETbody" type ="xs:string"/>
         </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>
Would someone help me to find the appropriate xsl file in order to manage to make this transformation possible ? Thank you very much for any help.
Best regards.
P.
0
Comment
Question by:earcelor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Expert Comment

by:sparkplug
ID: 8014987
Hi,

You didn't close or even respond to your previous question: http://www.experts-exchange.com/Web/Web_Languages/XML/Q_20496036.html. Were you satisfied with the answer? If so click on the accept answer button. Then we will be more eager to give you an answer to this one.

>S'Plug<
0
 
LVL 9

Accepted Solution

by:
sparkplug earned 400 total points
ID: 8015439
Hi,

You didn't say what to do with xs:element where the fixed attribute exist is a value other than 1000 or 0001, but I think this does what you want?

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
    <xsl:template match="@*|node()">
         <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
    </xsl:template>

    <!--blank template for element to delete-->
    <xsl:template match="xs:element[@fixed = '1000']" />

    <!--change name attribute for element a fixed attribute of 0001-->
    <xsl:template match="xs:element[@fixed = '0001']/@name" >
         <xsl:attribute name="name">TARGET<xsl:value-of select="."/></xsl:attribute>
    </xsl:template>

    <!--delete fixed attribute for element a fixed attribute of 0001-->
    <xsl:template match="xs:element[@fixed = '0001']/@fixed" />

</xsl:stylesheet>

You may also want to change the last bit to:

    <!--delete all fixed attributes-->
    <xsl:template match="@fixed" />

This will delete all fixed attributes without condition

Hope this helps,

>S'Plug<


0
 

Author Comment

by:earcelor
ID: 8015583
This XSL doesn' work because I received an error message while parsing:

" xsl:stylesheet may not contain PCDATA nodes."

What's wrong with this ?

Thanky you for your help again.

P
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8015664
This error meesage happens if there is some text within <xsl:stylesheet> tags. Are you sure you copied the above xsl correctly and didn't acccidently add any other characters?

>S'Plug<


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question