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 May 4th, 2007, 03:30 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Loop through XML?

Hi there,

how do I loop through an XML document, and change/delete only <gco:CharacterString> elements?

The other part of the document should be untouched.

The problem is, I cannot display the exact copy of XML (doing XSLT XML -> XML, Xalan) and the search criteria doesn't seem to work:
Code:
    <xsl:template match="//gco:CharacterString"> 
    <xsl:value-of select="./text()"/>
Any guidelines here?

 
Old May 4th, 2007, 03:56 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need the identity template with an extra template to match gco:CharacterString.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:gco="gco namespace uri goes here">

  <xsl:template match="node()|@*">

    <xsl:copy>

      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="gco:CharacterString">

  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
 
Old May 4th, 2007, 06:17 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,

the code you wrote does find some of the elements, but <gco:CharacterString>. And it returns me plain text, without any tree structure...

 
Old May 4th, 2007, 06:33 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I re-wrote the code:
Code:
    <xsl:template match="* | @* | text()">

        <xsl:copy>
            <xsl:apply-templates select="* | @* | text()"/>
        </xsl:copy>

    </xsl:template>
Now I am able to make a copy of my tree and I am able to see nodes, attributes and text as an output result.

The only question -- <gco:CharacterString> removal -- is still vague for me.

 
Old May 4th, 2007, 08:10 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The change you made is somewhat irrelevant as the node() test includes the text() test. Why not show the source XML and clarify what you want to happen to the <gco:CharacterString> element. Do you want it removed, along with its children or just removed?

As to the output format I'm not sure how you are trying to view it but I have used the identity template countless times and it always returns XML.

--

Joe (Microsoft MVP - XML)
 
Old May 4th, 2007, 09:20 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, say we got a chunk of code, which is more than enough, I think:
Code:
<citation>
   <CI_Citation>
      <title xsi:type="PT_FreeText_PropertyType">
         <gco:CharacterString>Dataset of the TDEM measurement campaign in Uveghuta, 2002 
         </gco:CharacterString>
      <PT_FreeText>
         textGroup # 13
      </PT_FreeText>
      </title>
   </CI_Citation>
</citation>
Currently, I am doing a transformation, using XSLT, of course. The main goal is to transform the XML document, to another XML document, removing some unnecessary tags or whatsoever. So, long story short, this is what I want to do (quite self-explanatory code):
Code:
<citation>
   <CI_Citation>
      <title xsi:type="PT_FreeText_PropertyType">
         Dataset of the TDEM measurement campaign in Uveghuta, 2002
      <PT_FreeText>
         textGroup # 13
      </PT_FreeText>
      </title>
   </CI_Citation>
</citation>
Well, at least I have moved one step further by being able to transform the exact copy of the document.

Sorry if those questions look too basic. It's just that I need to do this quick transformation. And with the knowledge of XSLT for a couple of days along with reading half of the book, sometimes I found myself stuck at it.

Any input is appreciated.
 
Old May 5th, 2007, 12:45 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, another step further.

The recent XSL code:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:default="http://www.isotc211.org/2005/gmd"
    xmlns:gco="http://www.isotc211.org/2005/gco" 
    xmlns:gml="http://www.opengis.net/gml" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:output method="xml"/>


    <xsl:variable name="lang">#xpointer(//*[@id='hu'])</xsl:variable>
    <xsl:variable name="lang_hu" select="//identificationInfo//title[@locale]"/>

    <xsl:template match="* | @* | text()">

        <xsl:copy>
            <xsl:apply-templates select="* | @* | text()"/>
            <xsl:apply-templates select="gco:CharacterString"/>
        </xsl:copy>

        <xsl:if test="$lang = $lang_hu">
            <xsl:value-of select="//@locale/text()"/>
        </xsl:if>

    </xsl:template>

        <xsl:template match="gco:CharacterString">

        </xsl:template> 


</xsl:stylesheet>
Don't mind the declaration and usage of variables. Still got to work on it.

The thing is, now I can display all the document along with _deleted_ <gco:CharacterString>. What I want to do, is preserve the text value of <gco:CharacterString> and move it one branch higher.

I know I have to improve the bolded template probably this way:
(1) copy the text value of <gco:CharacterString>
(2) delete the node
(3) insert the text value one step higher, probably the parent branch (using ".." I guess?)

Or am I misunderstanding the algorithm?


Andy
 
Old May 5th, 2007, 02:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can just use the template I suggested originally. Just change the override to:
Code:
<xsl:template match="gco:CharacterString">
  <xsl:appply-templates/>
</xsl:template>

--

Joe (Microsoft MVP - XML)
 
Old May 5th, 2007, 06:03 AM
Authorized User
 
Join Date: May 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nice.

Thanks Joe






Similar Threads
Thread Thread Starter Forum Replies Last Post
loop through a XML document and print out crmpicco Perl 1 November 13th, 2006 08:51 AM
Producing a loop of XML in CSV niceguycarl XSLT 0 February 6th, 2006 12:22 PM
Writing XML file within Loop (c#) rathbird General .NET 0 December 13th, 2004 05:26 PM
Problem with For loop, iterating through XML Doc rj_mayer Classic ASP XML 2 April 6th, 2004 01:16 PM
Loop in XML DOM? omallec XML 3 November 27th, 2003 09:16 AM





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