如何根据 XSL 1.0 中的元素填充值

How to populate a value based on an element in XSL 1.0

我的源样本XML如下

<QuoteData>
    <Components>
        <Component  ServiceOfferingId="XX"  StartDate="07/03/2016"  EndDate="12/31/9999"  SerialOrderNbr="xx"  StopReasonCode=""  IsBundle="N">
            <ServiceItem  ItemType="xx"  Type="2072"  ModelFeature="24E"  ProductId="2072 24E"  SerialOrderNbr="xx"  Quantity="1"  StartDate="07/03/2016"  EndDate="12/31/9999"  CustomerId="xx"  ServiceLevelId="xx"/>
        </Component>
        <Component  ServiceOfferingId="yy"  StartDate="07/03/2016"  EndDate="12/31/9999"  SerialOrderNbr="yy"  StopReasonCode=""  IsBundle="N">
            <ServiceItem  ItemType="yy"  Type="2072"  ModelFeature="24C"  ProductId="2072 24C"  SerialOrderNbr="yy"  Quantity="1"  StartDate="07/03/2016"  EndDate="12/31/9999"  CustomerId="yy"  ServiceLevelId="yy"/>
            <ServiceItem  ItemType="zz"  Type="2072"  ModelFeature="24E"  ProductId="2072 24E"  SerialOrderNbr="zz"  Quantity="1"  StartDate="07/03/2016"  EndDate="12/31/9999"  CustomerId="zz"  ServiceLevelId="zz"/>
        </Component>
    </Components>
    <Descriptions>
        <ProductDescription  Id="2072 24E"  Description="Customer EXPANSION"/>
        <ProductDescription  Id="2072 24C"  Description="Customer CONTROL"/>
    </Descriptions>
</QuoteData>

根据要求,每个 ServiceItem 都应转换为一行。因此,在上述情况下,应创建 3 行。 在目标中,我应该能够根据产品 ID 值填充产品描述。目前我无法使用 XSL 1.0

预期目标 XML 应如下所示:

<Payload>
    <Header></Header>
    <Line>
        <SerialOrderNbr>xx</SerialOrderNbr>
        <ModelFeature>24E</ModelFeature>
        <ProductId>2072 24E</ProductId>
        <ProductDescription>Customer EXPANSION</ProductDescription>
    </Line>  
    <Line>
        <SerialOrderNbr>xx</SerialOrderNbr>
        <ModelFeature>24C</ModelFeature>
        <ProductId>2072 24C</ProductId>
        <ProductDescription>Customer CONTROL</ProductDescription>
    </Line>
    <Line>
        <SerialOrderNbr>xx</SerialOrderNbr>
        <ModelFeature>24E</ModelFeature>
        <ProductId>2072 24E</ProductId>
        <ProductDescription>Customer EXPANSION</ProductDescription>
    </Line>
</Payload>

我尝试使用不同的函数来填充目标描述,但我要么得到所有行中的第一个描述值,要么根本没有填充任何值。我能够使用 while 循环实现所需的功能,并在 Jdev 11g 中分配 activity。

如何在 XSLT 中执行此操作?

使用以下样式表很容易:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" /> 
  <xsl:template match="/QuoteData">
    <Payload>
      <xsl:for-each select="//ServiceItem">
        <line>
          <SerialOrderNbr><xsl:value-of select="@SerialOrderNbr" /></SerialOrderNbr>
          <ModelFeature><xsl:value-of select="@ModelFeature" /></ModelFeature>
          <ProductId><xsl:value-of select="@ProductId" /></ProductId>
          <ProductDescription><xsl:value-of select="//ProductDescription[@Id = current()/@ProductId]/@Description" /></ProductDescription>
        </line>
      </xsl:for-each>
    </Payload>
  </xsl:template>    
</xsl:stylesheet>

它的输出是:

<?xml version="1.0"?>
<Payload>
    <line>
        <SerialOrderNbr>xx</SerialOrderNbr>
        <ModelFeature>24E</ModelFeature>
        <ProductId>2072 24E</ProductId>
        <ProductDescription>Customer EXPANSION</ProductDescription>
    </line>
    <line>
        <SerialOrderNbr>yy</SerialOrderNbr>
        <ModelFeature>24C</ModelFeature>
        <ProductId>2072 24C</ProductId>
        <ProductDescription>Customer CONTROL</ProductDescription>
    </line>
    <line>
        <SerialOrderNbr>zz</SerialOrderNbr>
        <ModelFeature>24E</ModelFeature>
        <ProductId>2072 24E</ProductId>
        <ProductDescription>Customer EXPANSION</ProductDescription>
    </line>
</Payload>

这与您声称的预期输出不完全相同,因为您对所需输出的版本似乎有误。在您的输出版本中,所有 SerialOrderNbr 都具有 'xx' 作为值,但在您的源代码中 XML 它们是不同的。