Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

xml - XSLT :Condition for check if null and empty element then only assign value to other element

We have input XML.In that,StockCode element is empty for many line items (OrderLineID elements). For all empty StockCode element in XML there must be Comment element value. Similarly,For all such empty Comment elements there must be StockCode Value Exist.

Note : OrderDetail is repeating Node here.

Scenario:

We have to split OrderDescription string . such a way that it always look for empty StockCode element. then only Assign split string values to Comment elements. Otherwise for all StockCode having value should not assign split string to Comment Element

INPUT XML :

<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
      <Orders>
        <OrderHeader>
          <Customer>000016</Customer>
          <OrderDate>2016-04-19</OrderDate>
          <SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
        </OrderHeader>
        <OrderDetails>
          <StockLine>
            <StockCode>ABB-CDE-FGH-01</StockCode>
            <OrderDescription>EDIORDER-SAVE COMMENTS
    C3 Generic
    LOC 0833
    Expected arrival 01/07/2016
     OTYPE NE
    TRKPC 01 GM/00007643020008361321</OrderDescription>
            <OrderLineID>OR-1561179</OrderLineID>
          </StockLine>
           <StockLine>
                    <StockCode>BCD-EFGH-01</StockCode>
                    <OrderLineID>OR-1561186</OrderLineID>
                    </Comment>
                  </StockLine>
          <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561180</OrderLineID>
          </StockLine>
          <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561181</OrderLineID>
          </StockLine>
          <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561182</OrderLineID>
          </StockLine>
          <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561183</OrderLineID>
          </StockLine>
          <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561184</OrderLineID>
          </StockLine>
           <StockLine>
            <StockCode></StockCode>
            </Comment>
            <OrderLineID>OR-1561185</OrderLineID>
          </StockLine>
        </OrderDetails>
      </Orders>
    </SalesOrders>

Existing XSLT For Transformation:

XSLT2.0

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="StockLine">
        <xsl:variable name="i" select="position()" />
        <xsl:copy>
            <xsl:copy-of select="StockCode"/>
            <Comment>
                <xsl:value-of select="normalize-space(tokenize(../StockLine[1]/OrderDescription, '
')[$i])"/>
            </Comment>
            <xsl:copy-of select="OrderLineID"/>
        </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

Expected Output value:

<SalesOrders xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="SORTOIDOC.XSD">
              <Orders>
                <OrderHeader>
                  <Customer>000016</Customer>
                  <OrderDate>2016-04-19</OrderDate>
                  <SalesForceOrderNumber>ORD-411324</SalesForceOrderNumber>
                </OrderHeader>
                <OrderDetails>
                  <StockLine>
                    <StockCode>ABB-CDE-FGH-01</StockCode>
                    </Comment>
                    <OrderDescription>EDIORDER-SAVE COMMENTS
            C3 Generic
            LOC 0833
            Expected arrival 01/07/2016
             OTYPE NE
            TRKPC 01 GM/00007643020008361321</OrderDescription>
                    <OrderLineID>OR-1561179</OrderLineID>
                  </StockLine>
                   <StockLine>
                    <StockCode>BCD-EFGH-01</StockCode>
                    <OrderLineID>OR-1561186</OrderLineID>
                    </Comment>
                  </StockLine>
                  <StockLine>
                    <Comment>EDIORDER-SAVE COMMENTS</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561180</OrderLineID>
                  </StockLine>
                  <StockLine>
                    <Comment>C3 Generic</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561181</OrderLineID>
                  </StockLine>
                  <StockLine>
                    <Comment>LOC 0833</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561182</OrderLineID>
                  </StockLine>
                  <StockLine>
                    <Comment>Expected arrival 01/07/2016</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561183</OrderLineID>
                  </StockLine>
                  <StockLine>
                    <Comment> OTYPE NE</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561184</OrderLineID>
                  </StockLine>
                  <StockLine>
                    <Comment>TRKPC 01 GM/00007643020008361321</Comment>
                    </StockCode>
                    <OrderLineID>OR-1561185</OrderLineID>
                  </StockLine>
                </OrderDetails>
              </Orders>
            </SalesOrders>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my suggestion tokenizing once and passing on the value as a parameter:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="OrderDetails">
        <xsl:copy>
            <xsl:variable name="descriptions" as="xs:string*" select="tokenize(StockLine[1]/OrderDescription, '
')"/>
            <xsl:apply-templates>
                <xsl:with-param name="descriptions" as="xs:string*" select="$descriptions" tunnel="yes"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="StockLine/StockCode[not(normalize-space())]">
        <xsl:param name="descriptions" tunnel="yes"/>
        <xsl:variable name="pos" as="xs:integer">
            <xsl:number count="StockLine[StockCode[not(normalize-space())]]"/>
        </xsl:variable>
        <xsl:copy>
            <xsl:value-of select="normalize-space($descriptions[$pos])"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

If you want to create new Comment elements instead of populating the empty StockCode then use an approach like this:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="OrderDetails">
        <xsl:copy>
            <xsl:variable name="descriptions" as="xs:string*" select="tokenize(StockLine[1]/OrderDescription, '
')"/>
            <xsl:apply-templates>
                <xsl:with-param name="descriptions" as="xs:string*" select="$descriptions"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="StockLine[StockCode[not(normalize-space())]]">
        <xsl:param name="descriptions"/>
        <xsl:variable name="pos" as="xs:integer">
            <xsl:number count="StockLine[StockCode[not(normalize-space())]]"/>
        </xsl:variable>
        <xsl:copy>
            <xsl:copy-of select="*"/>
            <Comment><xsl:value-of select="normalize-space($descriptions[$pos])"/></Comment>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...