What's the best way to only output a tag if it exists in XSL?

Posted by Morinar on Stack Overflow See other posts from Stack Overflow or by Morinar
Published on 2010-04-07T20:32:11Z Indexed on 2010/04/07 20:33 UTC
Read the original article Hit count: 241

Filed under:
|
|
|

I'm working on an interface with a 3rd party app that basically needs to take XML that was spat out by the app and convert it into XML our system can deal with. It's basically just applying a stylesheet to the original XML to make it looks like "our" XML. I've noticed that in other stylesheets we have, there are constructs like this:

<xsl:for-each select="State">
    <StateAbbreviation>
        <xsl:value-of select="."/>
    </StateAbbreviation>
</xsl:for-each>

Basically, the "in" XML has a State tag that I need to output as our recognized StateAbbreviation tag. However, I want to ONLY output the StateAbbreviation tag if the "in" XML contains the State tag. The block above accomplishes this just fine, but is not very intuitive (at least it wasn't to me), as every time I see a for-each I assume there is more than one, whereas in these cases there is 0 or 1.

My question: is that a standard-ish construct? If not, is there a more preferred way to do it? I could obviously check the string length (which is also being done in other stylesheets), but would like to do it the same, "best" way everywhere (assuming of course that a "best" way exists. Advice? Suggestions?

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt