문제

I have the following field (f28) and would like to show all results that have data. Being that already has the template to show a value and also not show value = null

My xsl.. for each field

 <xsl:template match="f28">
    <xsl:choose>
       <xsl:when test="ROW/@f1 !='NULL'">
        <xsl:element name="hasTestDegree" namespace="{namespace-uri()}#">
        <xsl:value-of select='ROW/@f1'/>
        </xsl:element>
       </xsl:when>
    </xsl:choose>
 </xsl:template>

My XML:

<f28>
 <ROW f1='FULL' f2='NULL' f3='NULL' f4='NULL' f5='not certain' f6='BRCA1' f7='NULL'/>
 <ROW f1='FULL' f2='NULL' f3='NULL' f4='NULL' f5='no mutation' f6='BRCA2' f7='NULL'/> 
 <ROW f1='FULL' f2='NULL' f3='NULL' f4='NULL' f5='NULL' f6='p53' f7='NULL'/>
</f28>

I wanted the following results :

  <hasTestDegree>FULL</hasTestDegree>
  <hasTestResult>not certain</hasTestResult>
  <hasTestType>BRCA1</hasTestType>`

  <hasTestDegree>FULL</hasTestDegree>
  <hasTestResult>no mutation</hasTestResult>
  <hasTestType>BRCA2</hasTestType>`

  <hasTestDegree>FULL</hasTestDegree>
  <hasTestType>p53</hasTestType>`
도움이 되었습니까?

해결책

  1. Create templates that match the attributes in question.
  2. Apply templates to the attributes.
  3. That's all.

Something like this:

<xsl:template match="f28">
  <xsl:apply-templates select="ROW" />
</xsl:template>

<xsl:template match="ROW">
  <xsl:apply-templates select="@*">
    <xsl:sort select="name()" />
  </xsl:apply-templates>
</xsl:template>

<!-- @f1 becomes <hasTestDegree> -->
<xsl:template match="f28//@f1">
  <hasTestDegree>
    <xsl:value-of select="." />
  </hasTestDegree>
</xsl:template>

<!-- add more templates for the other attributes... -->

<!-- any attribute with a value of 'NULL' is not output -->
<xsl:template match="@*[. = 'NULL']" />

Notes.

  • It seems unnecessary to use <xsl:element> in your case, just write out the element you want to create.
  • My solution relies on match specificity. The match expression @*[. = 'NULL'] overrules f28//@f1 for attributes that are 'NULL'.
  • The intermediate step (<xsl:apply-templates select="ROW" />) is necessary to make sure everything is processed in the right order.
  • You could either use <xsl:sort> in <xsl:apply-templates> or use <xsl:apply-templates> several times in a row, if you want to determine output order manually.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top