English 中文(简体)
如何根据使用XSL的属性提取价值
原标题:How to extract value using according to attribute using XSL
  • 时间:2012-01-12 20:33:10
  •  标签:
  • xml
  • xslt

Delivery document xml contains two <SourceDocument> tags:

<SourceDocument type="order">

以及

<SourceDocument type="desadv">

我试图从<代码><、SourceDocument类型=“order”>、上提取编号。

    <xsl:element name="tellimus">
        <xsl:value-of select="../../RefInfo/SourceDocument/SourceDocumentNum"/>
    </xsl:element>

但是,由此产生的xml文档中的要素是空的。 如何从<代码><、SourceDocument类型=“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" st以及alone="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" st以及alone="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>
最佳回答

两点:

  • There is a DocumentInfo node missing in your path
  • It would be better to specifically target the order element, instead of relying on an implicit node-set string conversion

使用以下表述:

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

只有作出这一改动才能产生以下产出(计划):

<VFPData>
   <Document-Order>
      <tellimus>OR51500044007</tellimus>
   </Document-Order>
</VFPData>
问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签