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 February 17th, 2013, 01:42 PM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default Best approach(es) for processing varying substrings

Hi all,

I'm wondering if anyone could share their approaches for dealing with lots of
variance in substrings. I have an XML file that looks something like:
Code:
<root>
    <row>
        <subj>foo, bar, baz</subj>
        <subj>qux, grault ==> fred, thud</subj>
    </row>
    <row>
        <subj>waldo, wobble</subj>
        <subj>HIDDEN: garply</subj>
    </row>
    <row>
        <subj>spam, hash</subj>
        <subj>eggs ==> bacon</subj>
        <subj>HIDDEN: garply, wibble ==> wubble</subj>
    </row>
    <row>
        <subj>qux ==> delete</subj>
    </row>
</root>
I'm processing it with an identity transform like so:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="subj">
        <xsl:for-each select="tokenize(., ',')">
            <subject>
                <xsl:value-of select="normalize-space(.)"/>
            </subject>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>
The above stylesheet gives me the following XML:
Code:
<root>
   <row>
      <subject>foo</subject>
      <subject>bar</subject>
      <subject>baz</subject>
      <subject>qux</subject>
      <subject>grault ==&gt; fred</subject>
      <subject>thud</subject>
   </row>
   <row>
      <subject>waldo</subject>
      <subject>wobble</subject>
      <subject>HIDDEN: garply</subject>
   </row>
   <row>
      <subject>spam</subject>
      <subject>hash</subject>
      <subject>eggs ==&gt; bacon</subject>
      <subject>HIDDEN: garply</subject>
      <subject>wibble ==&gt; wubble</subj>
   </row>
   <row>
      <subject>qux ==&gt; delete</subject>
   </row>
</root>
which is very close. But I'd like to be able to render the HIDDEN tokens to one line. E.g.
Code:
<subject>eggs ==&gt; bacon</subject>
<subject>HIDDEN: garply, wibble ==&gt; wubble</subject>
Is there a way to test tokens and group them? Would my approach be better if I used xsl:for-each-group? Thanks, in advance, for any insights you're willing to share.
 
Old February 17th, 2013, 01:51 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can certainly write a template
Code:
<xsl:template match="subj[starts-with(., 'HIDDEN:')]">
  <subject>
      <xsl:value-of select="."/>
  </subject>
</xsl:template>
then for your template with ' match="subj" ' exclude those e.g
Code:
<xsl:template match="subj[not(starts-with(., 'HIDDEN:'))]">
  <xsl:for-each select="tokenize(...)">...</xsl:for-each>
</xsl:template>
Additionally there is the regular expression support with the "matches" function which would even give you more control if needed.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 17th, 2013, 09:16 PM
Authorized User
 
Join Date: Jan 2012
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Martin, et al.,
Thanks for the reply - I forgot about using templates like that.

I've tried to take the `template match="subjects[contains(., 'HIDDEN')]"` a step further, but I'm running into a string-join() issue. The string '==>' denotes a change and I thought I could apply the following template to process those changes. I tokenize the subjects in a variable, and then, after creating a second variable, I try to apply a choose/when/otherwise on the resulting tokens. I want to discard tokens that contain '[Dd]elete' and update the tokens that indicate 'Old value ==> New value'. After that, I'm trying to put them back together. I can put them back together, but I can't get string-join() to insert commas between the values.
XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
        exclude-result-prefixes="#all">
        
        <xsl:output method="xml" indent="yes"/>
        <xsl:strip-space elements="*"/>
        
        <xsl:template match="/">
            <xsl:apply-templates/>
        </xsl:template>
        
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
        <xsl:template match="subjects[starts-with(., 'HIDDEN')]">
            <xsl:variable name="tHIDDEN" select="tokenize(., ',')"/>
            <xsl:variable name="tHIDDEN2">
                <xsl:for-each select="$tHIDDEN">
                    <xsl:choose>
                        <xsl:when test="contains(., '==&gt;') and not(contains(substring-after(., '==&gt;'), '[Dd]elete'))">
                            <xsl:value-of select="normalize-space(substring-after(., '==&gt;'))"/>
                        </xsl:when>
                        <xsl:when test="contains(., '==&gt;') and contains(substring-after(., '==&gt;'), '[Dd]elete')"/>
                        <xsl:otherwise>
                            <xsl:value-of select="."/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:variable>
            <xsl:for-each select="$tHIDDEN2">
                <subjectH2>
                    <xsl:value-of select="string-join(., ', ')"/>
                </subjectH2>
            </xsl:for-each>
        </xsl:template>
        
        <xsl:template match="subjects[not(starts-with(., 'HIDDEN'))]">
            <xsl:for-each select="tokenize(., ',')">
                <xsl:choose>
                    <xsl:when
                        test="contains(., '==&gt;') and not(contains(substring-after(., '==&gt;'), '[Dd]elete'))">
                        <subject>
                            <xsl:value-of select="normalize-space(substring-after(., '==&gt;'))"/>
                        </subject>
                    </xsl:when>
                    <xsl:otherwise>
                        <subject>
                            <xsl:value-of select="normalize-space(.)"/>
                        </subject>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:template>
        
    </xsl:stylesheet>
    
</xsl:stylesheet>
updated XML:
Code:
<root>
    <row>
        <subjects>foo, bar, baz</subjects>
        <subjects>qux, grault ==> fred, thud</subjects>
    </row>
    <row>
        <subjects>waldo, wobble</subjects>
        <subjects>HIDDEN: garply</subjects>
    </row>
    <row>
        <subjects>spam, hash</subjects>
        <subjects>eggs ==> bacon</subjects>
        <subjects>HIDDEN: garply, wibble ==> wubble</subjects>
    </row>
    <row>
        <subjects>wobble, wibble, wubble</subjects>
        <subjects>qux ==> delete</subjects>
    </row>
</root>
When I apply the provided template against the example XML, I get the following output:
Code:
<root>
   <row>
      <subject>foo</subject>
      <subject>bar</subject>
      <subject>baz</subject>
      <subject>qux</subject>
      <subject>fred</subject>
      <subject>thud</subject>
   </row>
   <row>
      <subject>waldo</subject>
      <subject>wobble</subject>
      <subjectH2>HIDDEN: garply</subjectH2>
   </row>
   <row>
      <subject>spam</subject>
      <subject>hash</subject>
      <subject>bacon</subject>
      <subjectH2>HIDDEN: garplywubble</subjectH2>
   </row>
   <row>
      <subject>wobble</subject>
      <subject>wibble</subject>
      <subject>wubble</subject>
      <subject>delete</subject>
   </row>
</root>
I also just noticed that my attempt at deleting the subjects containing ==> delete isn't working, either. I'm guessing that these are problems with the way I'm structuring my when tests, but I'm not sure how else to go about this.

Thanks, again, for any suggestions you provide.





Similar Threads
Thread Thread Starter Forum Replies Last Post
OPENGL ES Arri Forum and Wrox.com Feedback 0 February 3rd, 2013 11:41 AM
Problems in Listing 8-35: Asynchronous command processing using the callback approach Pegasus40218 BOOK: Professional ASP.NET 3.5 SP1 Edition: In C# and VB 2 May 29th, 2011 02:20 PM
XSLT substrings chroniclemaster1 XSLT 1 October 4th, 2007 05:29 PM





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