如何使用XSL根据属性提取值

时间:2012-01-12 20:33:11

标签: xml xslt

交付文档xml包含两个<SourceDocument>标记:

<SourceDocument type="order">

<SourceDocument type="desadv">

我尝试使用

<SourceDocument type="order">中提取订单号
    <xsl:element name="tellimus">
        <xsl:value-of select="../../RefInfo/SourceDocument/SourceDocumentNum"/>
    </xsl:element>

但生成的xml文件中的tellimus元素为空。如何从<SourceDocument type="order"><tellimus>元素提取订单号?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<E-Document>
  <Header>
    <DateIssued>2008-04-07</DateIssued>
    <SenderID>100</SenderID>
    <ReceiverID>-</ReceiverID>
  </Header>
  <Document>
    <DocumentType>recadv</DocumentType>
    <DocumentParties>
      <DeliveryParty context="self">
        <PartyCode>100</PartyCode>
        <Name>Selveri DC</Name>
      </DeliveryParty>
      <OrderParty context="self">
        <PartyCode>134</PartyCode>
        <Name>Torupilli Selver</Name>
      </OrderParty>
      <SellerParty context="partner">
        <PartyCode>-</PartyCode>
        <Name>Tarnija</Name>
      </SellerParty>
    </DocumentParties>
    <DocumentInfo>
      <DocumentNum>200015496</DocumentNum>
      <DateInfo>
        <DeliveryDateActual>2008-04-07</DeliveryDateActual>
        <ProcessingDate>2010-05-11</ProcessingDate>
      </DateInfo>
      <RefInfo>
        <SourceDocument type="order">
          <SourceDocumentNum>OR51500044007</SourceDocumentNum>
        </SourceDocument>
        <SourceDocument type="desadv">
          <SourceDocumentNum>DA51500044007</SourceDocumentNum>
        </SourceDocument>
      </RefInfo>
    </DocumentInfo>
    <DocumentItem>
      <ItemEntry>
        <LineItemNum>1</LineItemNum>
        <SellerItemCode>11001</SellerItemCode>
        <GTIN>4740125110012</GTIN>
        <ItemDescription>Piim 2.5% 1L kile</ItemDescription>
        <ItemUnitRecord>
          <ItemUnit>tk</ItemUnit>
        </ItemUnitRecord>
        <BaseUnit>tk</BaseUnit>
        <AmountOrdered>40.000</AmountOrdered>
        <AmountActual>40.000</AmountActual>
        <ItemReserve>
          <LotNum>04.03.2011</LotNum>
          <BestBeforeMin>2011-03-04</BestBeforeMin>
          <SerialNum/>
          <ItemReserveUnit>
            <ItemUnit>tk</ItemUnit>
            <AmountActual>40.000</AmountActual>
          </ItemReserveUnit>
        </ItemReserve>
      </ItemEntry>
    </DocumentItem>
  </Document>
</E-Document>

XSL:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" standalone="yes"/>

    <xsl:template match="/">
        <xsl:element name="VFPData">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <!-- this will denormalize XML data -->
    <xsl:template match="/E-Document/Document/DocumentItem/*">
        <xsl:element name="Document-Order">
            <!-- TODO: how to extract order number    -->
            <xsl:element name="tellimus">
                <xsl:value-of select="../../RefInfo/SourceDocument/SourceDocumentNum"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- to ommit nodes data -->
    <xsl:template match="text()">
    </xsl:template>

    <!-- to work over every node -->
    <xsl:template match="*">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

两点:

  • 您的路径中缺少DocumentInfo个节点
  • 最好专门定位order元素,而不是依赖于隐式节点集字符串转换

使用以下表达式:

../../DocumentInfo/RefInfo/SourceDocument[@type='order']/SourceDocumentNum

仅进行该更改会产生以下输出(格式化):

<VFPData>
   <Document-Order>
      <tellimus>OR51500044007</tellimus>
   </Document-Order>
</VFPData>