开发者

XSLT: Build a String from nested nodes

开发者 https://www.devze.com 2023-02-16 22:16 出处:网络
I have this XML with nested nodes, I would like to build a string out from it. How can I do that? sample XML node:

I have this XML with nested nodes, I would like to build a string out from it. How can I do that?

sample XML node:

<ND FID="22" FN="&gt;" FC="Compare" DT="Greater Than" DTY="BOOL"
    CID="-1" PS="1" PF="ROOT" DN="True" GB="False" LK="False" PT="2">
    <ND FID="176" FN="Add_Months" FC="Date and Time" DT="AddMonths"
        DTY="DATETIME" CID="-1" PS="1" PF="&gt;" DN="False" GB="False"
        LK="False" PT="2">
        <ND FID="117" FN="TodayMinusDays" FC="Date and Time"
            DT="TodayMinusDays" DTY="DATETIME" CID="-1" PS="1"
            PF="Add_Months" DN="False" GB="False" LK="False" PT="2">
            <ND FID="-1" FN="" FC="" DT="2" DTY="INTEGER" CID="5"
                PS="1" PF="TodayMinusDays" DN="False" GB="False"
                LK="False" PT="3"/>
        </ND>
       开发者_如何学Go <ND FID="-1" FN="" FC="" DT="10" DTY="INTEGER" CID="3"
            PS="2" PF="Add_Months" DN="False" GB="False" LK="False"
            PT="3"/>
    </ND>
    <ND FID="-1" FN="" FC="" DT="DTTM" DTY="DATETIME" CID="4" PS="2"
        PF="&gt;" DN="False" GB="False" LK="False" PT="4"/>
</ND>

expected output:

  Add_Months(TodayMinusDays(2), 10) > DTTM

Thanks in advance!


It looks like a parser result tree. This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="ND[@FID > 0][@DN='True']">
        <xsl:apply-templates select="*[1]"/>
        <xsl:value-of select="concat(' ',@FN,' ')"/>
        <xsl:apply-templates select="*[2]"/>
    </xsl:template>
    <xsl:template match="ND[@FID > 0][@DN='False']">
        <xsl:value-of select="concat(@FN,'(')"/>
        <xsl:for-each select="*">
            <xsl:if test="position()!=1"> ,</xsl:if>
            <xsl:apply-templates select="."/>
        </xsl:for-each>
        <xsl:text>)</xsl:text>
    </xsl:template>
    <xsl:template match="ND[0 > @FID]">
        <xsl:value-of select="@DT"/>
    </xsl:template>
</xsl:stylesheet>

Output:

Add_Months(TodayMinusDays(2) ,10) > DTTM

Note: Operator, function and data rules.


I can't test right now, but I guess it should be like this:

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

<xsl:template match="ND">
  <xsl:choose>
    <xsl:when test="@FN!=''">
        <xsl:value-of select="@FN" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="@DT" />
    </xsl:otherwise>
  </xsl:choose>
  (
  <xsl:apply-templates select="ND" />
  )
</xsl:template>
0

精彩评论

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