1

I have some very basic XML:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>One</string>
<string>Two</string>
</ArrayOfString>

How can I translate this into:

<ul>
 <li>One</li>
 <li>Two</li>
</ul>

Using XSLT?

Previously I've worked with a:value but these are just strings?

2 Answers 2

2

I'd do:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
>
  <xsl:template match="ArrayOfString">
    <ul>
      <xsl:apply-templates select="string" />
    </ul>
  </xsl:template>

  <xsl:template match="string">
    <li>
      <xsl:value-of select="." />
    </li>
  </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

1

Is something like this what you want?

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <ul>
    <xsl:apply-templates />
    </ul>
</xsl:template>
<xsl:template match="string">
    <li><xsl:value-of select="text()"></xsl:value-of></li>
</xsl:template>
</xsl:stylesheet>

It produces the following output for me:

<?xml version='1.0' ?>
<ul>
<li>One</li>
<li>Two</li>
</ul>

3 Comments

Your template for "/" works "by accident" only. You should make templates explicit instead of trusting implicit default rules to do the right thing. As soon as an actual template for ArrayOfString was defined elsewhere in the stylesheet, this solution would break.
Fair enough. I just started with the base XSL template my editor provided and added stuff until it worked :)
I have Stylus Studio 2007 - don't do much with XML these days so it's not been upgraded: stylusstudio.com/xml_product_index.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.