Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

XSL to filter XML based on attributes and using conditions

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
earcelor
Asked:
earcelor
  • 3
1 Solution
 
sparkplugCommented:
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
 
sparkplugCommented:
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
 
earcelorAuthor Commented:
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
 
sparkplugCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now