开发者

More efficent XSL

i have the following XSL that will transform an XML file and basically flatten it (remove all the sub nodes). However i dont think think it is very efficient, for example if there is no data for an element my XSL will still print out the enclosing tags, however what would be really nice, is if it didnt output any tags if there was no data in the XML file. Just wondering if someone 开发者_如何学编程could have a look and point me in the right direction please?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/*">
  <broadWorksCDR>
    <xsl:for-each select="cdrData">
      <cdrData>
        <serviceProvider><xsl:value-of select="headerModule/serviceProvider" /></serviceProvider>
        <type><xsl:value-of select="headerModule/type" /></type>
        <eventCounter><xsl:value-of select="headerModule/recordId/eventCounter" /></eventCounter>
        <systemId><xsl:value-of select="headerModule/recordId/systemId" /></systemId>
        <date><xsl:value-of select="headerModule/recordId/date" /></date>
        <systemTimeZone><xsl:value-of select="headerModule/recordId/systemTimeZone" /></systemTimeZone>
        <userNumber><xsl:value-of select="basicModule/userNumber" /></userNumber>
        <groupNumber><xsl:value-of select="basicModule/groupNumber" /></groupNumber>
        <direction><xsl:value-of select="basicModule/direction" /></direction>
        <callingNumber><xsl:value-of select="basicModule/callingNumber" /></callingNumber>
        <callingNumberContext><xsl:value-of select="basicModule/callingNumberContext" /></callingNumberContext>
      </cdrData>
    </xsl:for-each>
    </broadWorksCDR>
  </xsl:template>
</xsl:stylesheet>


You can use template, e.g.:

<xsl:template match="*">
    <xsl:if test=". != ''">
        <xsl:copy-of select="."/>
    </xsl:if>
</xsl:template>


Do you mean something like:

<xsl:if test="basicModule/groupNumber <> ''">
   <groupNumber><xsl:value-of select="basicModule/groupNumber" /></groupNumber>
</xsl:if>


You haven't provided a representative example of the source XML document and in this case the following transformation produces the wanted result:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8"
    indent="yes" omit-xml-declaration="no"/>

    <xsl:template match="/*">
        <broadWorksCDR>
          <xsl:apply-templates select="cdrData"/>
        </broadWorksCDR>
    </xsl:template>

    <xsl:template match="cdrData">
        <cdrData>
          <xsl:apply-templates select=
           "*[self::headerModule or self::basicModule]"/>
        </cdrData>
    </xsl:template>

    <xsl:template match=
     "*[self::serviceProvider or self::type or self::eventCounter
       or self::systemId or self::date or self::systemTimeZone
       or self::userNumber or self::groupNumber or self::direction
       or self::callingNumber or self::callingNumberContext]
                          [string-length() > 0]
     ">
         <xsl:element name="{name()}">
          <xsl:value-of select="."/>
         </xsl:element>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>

Explanation:

An element is created within the template that matches the wanted node -- this means that the wanted node exsists. The test for the wanted properties of the matched nodes is specified entirely within the match pattern, including test for the wanted elements and for the length of their string value being positive.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜