...">

How to make Coldfusion XSLT "contain" a function?

I am trying to convert xml dumps like this

<?xml version="1.0" encoding="UTF-8"?>
<report>
    <report_header>
        <c1>desc</c1>
        <c2>prname</c2>
        <c3>prnum</c3>
        <c4>cdate</c4>
        <c5>phase</c5>
        <c6>stype</c6>
        <c7>status</c7>
        <c8>parent</c8>
        <c9>location</c9>
    </report_header>
    <report_row>
        <c1></c1>
        <c2>IT Project Message Validation</c2>
        <c3>IT-0000021</c3>
        <c4>12/14/2010 09:56 AM</c4>
        <c5>Preparation</c5>
        <c6>IT Projects</c6>
        <c7>Active</c7>
        <c8>IT</c8>
        <c9>/IT/BIOMED</c9>
    </report_row>
    <report_row>
        <c1></c1>
        <c2>David, Michael John Morning QA Test</c2>
        <c3>IT-0000020</c3>
        <c4>12/14/2010 08:12 AM</c4>
        <c5>Preparation</c5>
        <c6>IT Projects</c6>
        <c7>Active</c7>
        <c8>IT</c8>
        <c9>/IT/BIOMED</c9>
    </report_row>
</report>

with xslt below, csv. Unfortunately, the contains function does not work.

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

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

    <xsl:template match="report">
        <xsl:apply-templates select="report_header"/>
        <xsl:apply-templates select="report_row"/>
    </xsl:template>

    <xsl:template match="report_header">
        <xsl:for-each select="*">
            <xsl:value-of select="."/>
            <xsl:if test="position() != last()">
                <xsl:value-of select="','"/>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>
        </xsl:text>
    </xsl:template>

    <xsl:template match="report_row">
        <xsl:param name="value" />
        <xsl:for-each select="*">
            <xsl:value-of select="$value" />
            <xsl:if test="(contains($value,','))">
                <xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text>
            </xsl:if>
            <xsl:if test="not(contains($value,','))">
                <xsl:value-of select="."/>
            </xsl:if>
            <xsl:if test="position() != last()">
                <xsl:value-of select="','"/>
            </xsl:if>
        </xsl:for-each>
        <xsl:if test="position() != last()">
            <xsl:text>
            </xsl:text>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

I get the next landfill. I was expecting apostates around the prname column in the second row.

desc,prname,prnum,cdate,phase,stype,status,parent,location
        ,IT Project Message Validation,IT-0000021,12/14/2010 09:56 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED
            ,David, Michael John Morning QA Test,IT-0000020,12/14/2010 08:12 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED

I just used the xmltransform coldfusion function to test it.

+3
source share
2 answers

I do not think that contains()is your problem.

, report_row <xsl:param name="value"/>, . , , . $value , contain() , .

, select xsl:param:

  <xsl:template match="report_row">
        <xsl:param name="value" select="." />

, "push" , , "pull", XSLT.

- : :

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates select="*/report_header/*"/>
        <xsl:apply-templates select="*/report_row/*"/>
    </xsl:template>

    <!-- For all but the last item, apply templates for the content, then add a comma -->
    <xsl:template match="*[following-sibling::*]">
        <xsl:apply-templates/>
        <xsl:text>,</xsl:text>
    </xsl:template>

    <!-- If it the last element in a group, add a newline char -->
    <xsl:template match="*[not(following-sibling::*)]">
       <xsl:apply-templates />
        <!--Line break-->
        <xsl:text>&#10;</xsl:text>
    </xsl:template>

    <!-- If any values contains a comma, wrap it in quotes -->
    <xsl:template match="text()[contains(.,',')]">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
    </xsl:template>

</xsl:stylesheet>

:

desc,prname,prnum,cdate,phase,stype,status,parent,location
,IT Project Message Validation,IT-0000021,12/14/2010 09:56 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED
,"David, Michael John Morning QA Test",IT-0000020,12/14/2010 08:12 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED
+1

, .

, .

, :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="report_header/*">
            <xsl:value-of select="."/>
            <xsl:call-template name="processEnd"/>
    </xsl:template>

    <xsl:template match="report_row/*[contains(., ',')]">
                <xsl:text>"</xsl:text>
                <xsl:value-of select="."/>
                <xsl:text>"</xsl:text>
            <xsl:call-template name="processEnd"/>
    </xsl:template>

    <xsl:template match="report_row/*[not(contains(., ','))]">
                <xsl:value-of select="."/>
            <xsl:call-template name="processEnd"/>
    </xsl:template>

    <xsl:template name="processEnd">
      <xsl:choose>
          <xsl:when test="position() != last()">,</xsl:when>
          <xsl:otherwise><xsl:text>             </xsl:text></xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

XML-:

<report>
    <report_header>
        <c1>desc</c1>
        <c2>prname</c2>
        <c3>prnum</c3>
        <c4>cdate</c4>
        <c5>phase</c5>
        <c6>stype</c6>
        <c7>status</c7>
        <c8>parent</c8>
        <c9>location</c9>
    </report_header>
    <report_row>
        <c1></c1>
        <c2>IT Project Message Validation</c2>
        <c3>IT-0000021</c3>
        <c4>12/14/2010 09:56 AM</c4>
        <c5>Preparation</c5>
        <c6>IT Projects</c6>
        <c7>Active</c7>
        <c8>IT</c8>
        <c9>/IT/BIOMED</c9>
    </report_row>
    <report_row>
        <c1></c1>
        <c2>David, Michael John Morning QA Test</c2>
        <c3>IT-0000020</c3>
        <c4>12/14/2010 08:12 AM</c4>
        <c5>Preparation</c5>
        <c6>IT Projects</c6>
        <c7>Active</c7>
        <c8>IT</c8>
        <c9>/IT/BIOMED</c9>
    </report_row>
</report>

, :

desc,prname,prnum,cdate,phase,stype,status,parent,location             ,IT Project Message Validation,IT-0000021,12/14/2010 09:56 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED             ,"David, Michael John Morning QA Test",IT-0000020,12/14/2010 08:12 AM,Preparation,IT Projects,Active,IT,/IT/BIOMED             
+2

Source: https://habr.com/ru/post/1781159/


All Articles