XSLT:检查长度并复制值

问题描述 投票:0回答:1

我正在尝试编写一个XSLT来检查

<identification>
,其中
<identificationType>
是否为Type10,如果存在并且
<identificationValue>
长度小于或等于5,那么我们需要替换Identificationvalue对于
<serialShippingContainerCode>
,如果identificationType=Type10不存在,那么我们需要使用从Serialshippingcontainercode获取的identificationvalue创建一个。

我使用的XSLT只是将标头复制到子级(如果它不存在),请在这里帮忙一次

输入:

<StandardBusinessDocument>
    <receivingAdvice>
        <LogisticUnitLineItem>
            <logisticUnitIdentification>
                <ContainerCode>
                    <serialShippingContainerCode>121212345890</serialShippingContainerCode>
                </ContainerCode>
            </logisticUnitIdentification>
            <ContainmentLine number="1">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>Value1</IdentificationValue>
                        <IdentificationType>Type8</IdentificationType>
                    </Identification>
                    <Identification>
                        <IdentificationValue>4567</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
            <ContainmentLine number="2">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>Value2</IdentificationValue>
                        <IdentificationType>Type8</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
        </LogisticUnitLineItem>
        <LogisticUnitLineItem>
            <logisticUnitIdentification>
                <ContainerCode>
                    <serialShippingContainerCode>1309089485</serialShippingContainerCode>
                </ContainerCode>
            </logisticUnitIdentification>
            <ContainmentLine number="1">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>86432509</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
            <ContainmentLine number="2">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>6867</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
        </LogisticUnitLineItem>
    </receivingAdvice>
</StandardBusinessDocument>

** 所需输出:**

<StandardBusinessDocument>
    <receivingAdvice>
        <LogisticUnitLineItem>
            <logisticUnitIdentification>
                <ContainerCode>
                    <serialShippingContainerCode>121212345890</serialShippingContainerCode>
                </ContainerCode>
            </logisticUnitIdentification>
            <ContainmentLine number="1">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>Value1</IdentificationValue>
                        <IdentificationType>Type8</IdentificationType>
                    </Identification>
                    <Identification>
                        <IdentificationValue>121212345890</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
            <ContainmentLine number="2">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>Value2</IdentificationValue>
                        <IdentificationType>Type8</IdentificationType>
                    </Identification>
                    <Identification>
                        <IdentificationValue>121212345890</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
        </LogisticUnitLineItem>
        <LogisticUnitLineItem>
            <logisticUnitIdentification>
                <ContainerCode>
                    <serialShippingContainerCode>1309089485</serialShippingContainerCode>
                </ContainerCode>
            </logisticUnitIdentification>
            <ContainmentLine number="1">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>86432509</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
            <ContainmentLine number="2">
                <containedItemIdentification>
                    <gtin>00000000000000</gtin>
                    <Identification>
                        <IdentificationValue>1309089485</IdentificationValue>
                        <IdentificationType>Type10</IdentificationType>
                    </Identification>
                </containedItemIdentification>
            </ContainmentLine>
        </LogisticUnitLineItem>
    </receivingAdvice>
</StandardBusinessDocument>

** 我使用的 XSLT 如下:**

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>  
 
  <xsl:template match="Identification[last()][not(contains(IdentificationType, 'Type10'))]">
    <xsl:copy-of select="."/>
    <xsl:copy>
      <IdentificationValue>
        <xsl:value-of select="ancestor::LogisticUnitLineItem/logisticUnitIdentification/ContainerCode/serialShippingContainerCode"/>
      </IdentificationValue>
      <IdentificationType>Type10</IdentificationType>
    </xsl:copy>
  </xsl:template> 
  
</xsl:stylesheet>
xslt-2.0 xslt-3.0
1个回答
0
投票

您可以使用两个模板来完成。

第一个搜索“Identification”并适当地替换“IdentificationValue”。

另一个搜索父节点containedItemIdentification,然后追加将Identification添加到其中。

所以请参阅下面我的建议。我不再解释它,因为如果您有点 XSLT,那么它会非常简单。在我看来,学习和思考 XSLT 是最难的事情之一。但请继续关注! :-) 一段时间后,您就会更好地了解如何解决实际问题。

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>  
 
  <xsl:template match="Identification">
    <xsl:copy>
      <xsl:choose>
        <xsl:when test="IdentificationType='Type10' and string-length(IdentificationValue) &lt; 5">
          <IdentificationValue>
            <xsl:value-of select="../../../logisticUnitIdentification/ContainerCode/serialShippingContainerCode"/>
          </IdentificationValue>
          <xsl:apply-templates select="IdentificationType"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="containedItemIdentification">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:if test="not(Identification/IdentificationType[.='Type10'])">
        <Identification>
          <IdentificationValue>
            <xsl:value-of select="../../logisticUnitIdentification/ContainerCode/serialShippingContainerCode"/>
          </IdentificationValue>
          <IdentificationType>Type10</IdentificationType>
        </Identification>
     </xsl:if>    
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.