Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 October 20th, 2006, 02:21 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to venjamin
Default filtering xml in using XSLT

Dear

i am tring to do it, but it does not work, i was gone for a week
if name element in source.xml is A, go to Message1, if name element is B go to Message2.

please help me with xslt coding i wrriten

source.xml
---------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>
        <ns1:bpm_out_mt xmlns:ns1="http://afes.temp.com/test01">
            <details>
                <name>A</name>
                <id>12313</id>
            </details>
            <details>
                <name>B</name>
                <id>13131</id>
            </details>
            <details>
                <name>A</name>
                <id>55433</id>
            </details>
        </ns1:bpm_out_mt>
    </ns0:Message1>
</ns0:Messages>


xslt i am tring
----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://afes.temp.com/test01">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
        <xsl:if test="ns0:Messages/ns0:Message1/ns1:bpm_out_mt/details/name = A">
        <ns0:Message1>
        <ns1:bpm_in01_mt xmlns:ns1="http://afes.temp.com/test01">
            <xsl:for-each select="ns0:Messages/ns0:Message1/ns1:bpm_out_mt/details">
            <info>
                    <xsl:value-of select="name" />
                    <xsl:value-of select="id" />
                </info>
            </xsl:for-each>
            </ns1:bpm_in01_mt>
        ?lt;/ns0:Message1>
        </xsl:if>
        <xsl:if test="ns0:Messages/ns0:Message1/ns1:bpm_out_mt/details/name = B">
        <ns0:Message2>
        <ns1:bpm_in02_mt xmlns:ns1="http://afes.temp.com/test01">
            <xsl:for-each select="ns0:Messages/ns0:Message1/ns1:bpm_out_mt/details">
            <info>
                    <xsl:value-of select="name" />
                    <xsl:value-of select="id" /> ?
            </info>
            </xsl:for-each>
        </ns1:bpm_in02_mt>
        </ns0:Message2>
        </xsl:if>
    ?lt</ns0:Messages>
    </xsl:template>
</xsl:stylesheet>


target.xml i wish
----------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<ns1:bpm_in01_mt xmlns:ns1="http://afes.temp.com/test01">
<info>
<name>A</name>
<id>12313</id>
</info>
<info>
<name>A</name>
<id>55433</id>
</info>
</ns1:bpm_in01_mt>
</ns0:Message1>

<ns0:Message2>
<ns1:bpm_in02_mt xmlns:ns1="http://afes.temp.com/test01">
<info>
<name>B</name>
<id>13131</id>
</info>
</ns1:bpm_in02_mt>
</ns0:Message2>
</ns0:Messages>
 
Old October 20th, 2006, 04:21 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't understood two of the most basic concepts of XSLT: context, and template rules. This is what your code is doing:

<xsl:if test="ns0:Messages/..../details/name = A">
          <ns0:Message1>
          <ns1:bpm_in01_mt xmlns:ns1="http://afes.temp.com/test01">
              <xsl:for-each select="ns0:Messages/ns0:Message1/ns1:bpm_out_mt/details">

The xsl:if says "if there is an element selected by the path ns0:Messages/..../details/name whose value is equal to A then..." Your context node is the document root, so you are testing whether there is such an element anywhere in the document. If there is, then you do the xsl:for-each, which again selects from the document root, and thus processes all the selected elements in the whole document, whether or not they matched the condition in the xsl:if.

(There's another little mistake in your code as well, you should have said ='A' rather than =A. A without quotes means child::A, that is, an element whose name is A.)

What you want to achieve is to transform a details element with name=A intoa Message1, and to transform a details element with name=B into a message2. So write two template rules:

<xsl:template match="details[name='A']">
        <ns0:Message1>
          <ns1:bpm_in01_mt xmlns:ns1="http://afes.temp.com/test01">
              <info>
                     <xsl:value-of select="name" />
                     <xsl:value-of select="id" />
                 </info>
            </ns1:bpm_in01_mt>
         </ns0:Message1>
</xsl:template>

and similarly for match="details[name='B']"

Then just make sure the details elements are processed:

<xsl:template match="/">
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
  <xsl:apply-templates select="//detail"/>
</ns0:Messages>
</xsl:template>


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference




Similar Threads
Thread Thread Starter Forum Replies Last Post
Filtering XML data aldwinenriquez XSLT 7 August 25th, 2008 03:24 AM
XML Filtering BlkR XML 1 June 12th, 2008 05:25 PM
Help with filtering xml jconroy XSLT 6 April 6th, 2008 11:33 PM
Filtering an xml with xslt Tomi XSLT 4 September 5th, 2006 06:29 AM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM





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