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

耗尽温柔 提交于 2019-12-13 11:09:49

问题


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, '\n')[$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>

回答1:


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, '\n')"/>
            <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, '\n')"/>
            <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>



回答2:


To transform your example input you only need to do a template match on the StockCode node like so:

<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="StockCode[not(node())]">
    <xsl:variable name="i"><xsl:number count="StockCode[not(node())]"  select="../StockCode[not(node())]"  level="any" /></xsl:variable>
    <xsl:copy>
            <xsl:value-of select="normalize-space(tokenize(ancestor::OrderDetails/StockLine[1]/OrderDescription, '\n')[number($i)])"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

The template will only apply for StockCodes with no content. Since this is the only template all other nodes will remain the same in the output (due to the identity template). The matching template will count all previous StockCode nodes that are empty and use this as an index to locate the tokenized text of the description.



来源:https://stackoverflow.com/questions/38167552/xslt-condition-for-check-if-null-and-empty-element-then-only-assign-value-to-ot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!