คำตอบ looping through a group of items in a parser

  • 5 เมษายน 2555 21:41
     
     

    I am new too xml and stylesheets so be kind..<G>   I have xml results coming from Biztalk that I need to parse and format, including only those nodes that contain a value.  Each item will have up to 9 enteries.  Here is some sample of what I have

    <xsl:text> DOB/</xsl:text>
      <xsl:value-of select="//TRN/DOB"/>
    <xsl:call-template name="cr" />
      <xsl:text> DOB/</xsl:text>
      <xsl:value-of select="//TRN/DOB1"/>
    <xsl:call-template name="cr" />

       <xsl:text> LIC/</xsl:text>
          <xsl:value-of select="//TRN/LIC"/>
      <xsl:call-template name="cr" />

    I know I can do this

    <xsl:if test ='DOB[.!=""]'>
      <xsl:text> DOB/</xsl:text>
      <xsl:value-of select="//TRN/DOB"/>
    </xsl:if>

    and the node will not be displayed if it is blank, but I have 9 enteries for each item and 18 items to do this for.  I was told I might be able to do a loop for each item and only display if they have data.

    suggestions?

    Thanks

    Karl

ตอบทั้งหมด

  • 6 เมษายน 2555 11:53
     
     คำตอบ

    It sounds to me as using templates can help shorten the code e.g.

      <xsl:template match="TRN">

        <xsl:apply-templates/>

      </xsl:template>

      <xsl:template match="TRN/DOB | TRN/DOB1 | TRN/LIC">

        <xsl:if test="normalize-space()"> <!-- or <xsl:if test=". != ''"> -->

           <xsl:value-of select="local-name()"/>

           <xsl:text>&#10;</xsl:text>

        </xsl:if>

      </xsl:template>

    The first template for TRN which simply does apply-templates optimistically assumes you simply want to process its child elements in the order they appear in the XML input document, if that is not the case then you need to replace the single apply-templates with e.g.

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

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

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

    to enforce the processing order you need.

    That's my current suggestion on the approach to take, based on the input you have provided. If you still struggle then please consider to post a small but representative XML input sample and the corresponding output you want to create with XSLT, then I am sure we can help with an "XSLT like" approach to achieve that.


    MVP Data Platform Development My blog

    • ทำเครื่องหมายเป็นคำตอบโดย khalbert 11 เมษายน 2555 13:14
    •  
  • 10 เมษายน 2555 22:00
     
     

    Martin,

    I have that working, but it is slow.  I ended up using a combination due to the fact that I already had a template to find other nodes.

    Thanks

    Karl