Dee J. Doena
Dee J. Doena

Reputation: 1853

How to build a string with XSLT?

I have this XML snippet

<MediaTypes>
  <DVD>true</DVD>
  <HDDVD>false</HDDVD>
  <BluRay>true</BluRay>
</MediaTypes>

And I want to create an output that looks like this

DVD / Blu-ray

or like this

DVD

or like this

DVD / HD-DVD / Blu-ray

depending on the true/false states

But I'm a total beginner in the XSLT game. :-/

Here's a snippet of the full XML

<?xml version="1.0" encoding="Windows-1252"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DVD>
    <ProfileTimestamp>2012-06-07T05:20:51Z</ProfileTimestamp>
    <ID>0044005507027.5</ID>
    <MediaTypes>
      <DVD>true</DVD>
      <HDDVD>false</HDDVD>
      <BluRay>false</BluRay>
    </MediaTypes>
    <UPC>0-044005-507027</UPC>
    <CollectionNumber>448</CollectionNumber>
    <CollectionType IsPartOfOwnedCollection="true">Owned</CollectionType>
    <Title>The Big Lebowski</Title>
    <DistTrait />
    <OriginalTitle />
    <CountryOfOrigin>United States</CountryOfOrigin>
    ...
  </DVD>
  <DVD>
     ...
  </DVD>
</Collection>

I tried to follow the advices given below (regarding the loop), but it doesn't seem to work.

This is the XSL I have so far (complete):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template name="MediaTypes" match="MediaTypes">
        Test
        <xsl:for-each select="*[.='true']">            
            <xsl:value-of select="name()"/>
            <xsl:if test="position()!=last()">
                <xsl:text>/</xsl:text>
            </xsl:if>
        </xsl:for-each>        
    </xsl:template>

    <xsl:template match="/">
        <html>
            <head>
                <title>DVD Profiler Double Dips</title>
            </head>
            <body>
                <div>
                    <h1>DVD Profiler Double Dips</h1>
                </div>
                <table border="1" cellpadding="3" cellspacing="0">
                    <tr>
                        <th>Title</th>
                        <th>Edition</th>
                        <th>Production Year</th>
                        <th>DVD /</th>
                        <th>Purchase Date</th>
                        <th>Collection Type</th>
                        <th>Original Title</th>
                        <th>Sort Title</th>
                    </tr>
                    <xsl:for-each select="/Collection/DVD">
                        <tr>
                            <td>
                                <xsl:value-of select="Title"/>
                            </td>
                            <td>
                                <xsl:if test="DistTrait != ''">
                                    <xsl:value-of select="DistTrait"/>
                                </xsl:if>
                                <xsl:if test="DistTrait = ''">
                                    &#160;
                                </xsl:if>
                            </td>
                            <td>
                                <xsl:if test="ProductionYear != ''">
                                    <xsl:value-of select="ProductionYear"/>
                                </xsl:if>
                                <xsl:if test="ProductionYear = ''">
                                    &#160;
                                </xsl:if>
                            </td>
                            <td>
                                <xsl:call-template name="MediaTypes" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

But where

<xsl:call-template name="MediaTypes" />

is standing, only "Test" comes out

Yay!

You guys are awesome. Yes

<xsl:apply-templates select="MediaTypes"/>

Was my weapon of choice.

My final template looks like this:

<xsl:template name="MediaTypes" match="MediaTypes">
    <xsl:for-each select="*[.='true']">
        <xsl:value-of select="name()"/>
        <xsl:if test="position()!=last()">
            <xsl:text> / </xsl:text>
        </xsl:if>
    </xsl:for-each>
    <xsl:if test="CustomMediaType != ''">
        <xsl:if test="*[.='true']">
            <xsl:text> / </xsl:text>
        </xsl:if>
        <xsl:value-of select="CustomMediaType"/>
    </xsl:if>
</xsl:template>

begins to understand how stuff works

Thanks for all your help!

Upvotes: 1

Views: 2021

Answers (2)

Eric Herlitz
Eric Herlitz

Reputation: 26307

I'd advice you to change the xml a bit

Here is an example

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" ?>
<MediaTypes>
    <Media name="DVD">true</Media>
    <Media name="HDDVD">false</Media>
    <Media name="BluRay">true</Media>
</MediaTypes>

This way you may write XPATH expressions like //MediaTypes/Media However did @michael.hor257k write a nice solution, jacking this xml pattern into that would look like this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="//MediaTypes">
        <xsl:for-each select="*[.='true']">
            <xsl:value-of select="@name"/>
            <xsl:if test="position()!=last()">
                <xsl:text>/</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

This will render DVD/BluRay

Test here http://xslttest.appspot.com/

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117140

The question is rather trivial - the real problem is that you only show a snippet of the XML document. XSLT is very much context-dependent.

Here is a template to match any <MediaTypes> element and output the required string; hopefully you'll know how to incorporate it into your own stylesheet:

<xsl:template match="MediaTypes">
    <xsl:for-each select="*[.='true']">
        <xsl:value-of select="name()"/>
        <xsl:if test="position()!=last()">
            <xsl:text>/</xsl:text>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

Upvotes: 3

Related Questions