开发者

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

开发者 https://www.devze.com 2022-12-25 19:31 出处:网络
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 stylesh

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 s开发者_高级运维tylesheets 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?


You could replace the for-each with:

<xsl:if test="State">


It is a recommended good practice in XSLT to prefer using xsl:apply-templates over xsl:for-each.

In this case a simple

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

solves the problem in a most elegant way. No conditionals are necessary and the XSLT processor does all the work to apply the template best matching each State child.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号