这条XSLT代码有什么问题

时间:2013-08-03 10:37:05

标签: xslt-1.0

刚刚开始编码。属性值设置似乎不起作用,并且不访问外部for-each循环中的引用节点的子节点。我无法弄清楚代码有什么问题。

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <B2BGatewayDoc>
      <Document>
        <xsl:copy-of select="/B2BGatewayDoc/Document/ServiceHeader" />
        <ServiceContent>
          <Transmission>
            <TransmissionBody>
              <xsl:copy-of select="/B2BGatewayDoc/Document/ServiceContent/Transmission/TransmissionBody/ReceiverSystem" />
              <xsl:for-each select="/B2BGatewayDoc/Document/ServiceContent/Transmission/TransmissionBody/GLogXMLElement" >
                <GLogXMLElement>
                  <ActualShipment>
                    <xsl:attribute name="xmlns" >
                      <xsl:value-of select="ActualShipment/@xmlns" />
                    </xsl:attribute>
                    <Shipment>
                      <xsl:copy-of select="ActualShipment/Shipment/ShipmentHeader" />
                      <xsl:for-each select="ActualShipment/Shipment/TransOrder" >
                        <TransOrder>
                          <xsl:copy-of select="TransOrderHeader" />
                        </TransOrder>
                      </xsl:for-each>
                    </Shipment>
                  </ActualShipment>
                </GLogXMLElement>
              </xsl:for-each>
            </TransmissionBody>
          </Transmission>
        </ServiceContent>
      </Document>
    </B2BGatewayDoc>
  </xsl:template>
</xsl:stylesheet>

XML示例

<?xml version="1.0"?>
<B2BGatewayDoc>
  <Document>
    <ServiceHeader>
      <Protocol name="ANSIX12">
        <Sender>941714834TEST</Sender>
        <Receiver>003897733SAPT</Receiver>
        <MsgType>3A4MG2</MsgType>
        <ReferenceNumber/>
        <PipVersion>V02.02</PipVersion>
        <InstanceIdentifier/>
      </Protocol>
    </ServiceHeader>
    <ServiceContent>
      <Transmission>
        <TransmissionBody>
          <ReceiverSystem>
            <ReceiverSystemName>XXXXX</ReceiverSystemName>
            <ReceiverSystemName>YYYYY</ReceiverSystemName>
          </ReceiverSystem>
          <GLogXMLElement>
            <ActualShipment xmlns="1000181ZZZ">
              <Shipment>
                <ShipmentHeader>
                  <ShipmentGid>
                    <Gid>
                      <Xid>1000181ZZZ</Xid>
                    </Gid>
                  </ShipmentGid>
                  <ShipmentRefnum>
                    <ShipmentRefnumQualifierGid>
                      <Gid>
                        <Xid>SELLER_INV_NBR</Xid>
                      </Gid>
                    </ShipmentRefnumQualifierGid>
                    <ShipmentRefnumValue>VALUE 1</ShipmentRefnumValue>
                  </ShipmentRefnum>
                  <ShipmentRefnum>
                    <ShipmentRefnumQualifierGid>
                      <Gid>
                        <Xid>SHIP_TYPE</Xid>
                      </Gid>
                    </ShipmentRefnumQualifierGid>
                    <ShipmentRefnumValue>O</ShipmentRefnumValue>
                  </ShipmentRefnum>
                  <TransactionCode>IU</TransactionCode>
                  <TotalShipUnitCount>1</TotalShipUnitCount>
                  <TotalPackagedItemSpecCount>1</TotalPackagedItemSpecCount>
                  <CommercialTerms>
                    <IncoTermGid>
                      <Gid>
                        <Xid>EXW</Xid>
                      </Gid>
                    </IncoTermGid>
                  </CommercialTerms>
                  <NumOrderReleases>1</NumOrderReleases>
                  <VesselGid>
                    <Gid>
                      <Xid>BOL54891</Xid>
                    </Gid>
                  </VesselGid>
                </ShipmentHeader>
                <TransOrder>
                  <TransOrderHeader>
                    <TransOrderGid>
                      <Gid>
                        <Xid>BR20000238</Xid>
                      </Gid>
                    </TransOrderGid>
                    <CommercialTerms>
                      <IncoTermGid>
                        <Gid>
                          <Xid>EXW</Xid>
                        </Gid>
                      </IncoTermGid>
                    </CommercialTerms>
                    <TransportModeGid>
                      <Gid>
                        <Xid>H</Xid>
                      </Gid>
                    </TransportModeGid>
                    <ModeProfileGid>
                      <Gid>
                        <Xid/>
                      </Gid>
                    </ModeProfileGid>
                    <OrderRefnum>
                      <OrderRefnumQualifierGid>
                        <Gid>
                          <Xid>BOL</Xid>
                        </Gid>
                      </OrderRefnumQualifierGid>
                      <OrderRefnumValue>BOL54891</OrderRefnumValue>
                    </OrderRefnum>
                    <OrderRefnum>
                      <OrderRefnumQualifierGid>
                        <Gid>
                          <Xid>CUST_PO_NBR</Xid>
                        </Gid>
                      </OrderRefnumQualifierGid>
                      <OrderRefnumValue>4700175047</OrderRefnumValue>
                    </OrderRefnum>
                    <TotalNetWeightVolume>
                      <Weight>
                        <WeightValue>18.9</WeightValue>
                        <WeightUOMGid>
                          <Gid>
                            <Xid>KG</Xid>
                          </Gid>
                        </WeightUOMGid>
                      </Weight>
                      <Volume>
                        <VolumeValue>0</VolumeValue>
                        <VolumeUOMGid>
                          <Gid>
                            <Xid>CR</Xid>
                          </Gid>
                        </VolumeUOMGid>
                      </Volume>
                    </TotalNetWeightVolume>
                  </TransOrderHeader>
                </TransOrder>
              </Shipment>
            </ActualShipment>
          </GLogXMLElement>
        </TransmissionBody>
      </Transmission>
    </ServiceContent>
  </Document>
</B2BGatewayDoc>

我得到的输出

<?xml version='1.0' ?>
<B2BGatewayDoc>
  <Document>
    <ServiceHeader>
      <Protocol name="ANSIX12">
        <Sender>941714834TEST</Sender>
        <Receiver>003897733SAPT</Receiver>
        <MsgType>3A4MG2</MsgType>
        <ReferenceNumber/>
        <PipVersion>V02.02</PipVersion>
        <InstanceIdentifier/>
      </Protocol>
    </ServiceHeader>
    <ServiceContent>
      <Transmission>
        <TransmissionBody>
          <ReceiverSystem>
            <ReceiverSystemName>XXXXX</ReceiverSystemName>
            <ReceiverSystemName>YYYYY</ReceiverSystemName>
          </ReceiverSystem>
          <GLogXMLElement>
            <ActualShipment xmlns="">
              <Shipment/>
            </ActualShipment>
          </GLogXMLElement>
        </TransmissionBody>
      </Transmission>
    </ServiceContent>
  </Document>
</B2BGatewayDoc>

虽然源值可用,但未为属性xmlns分配值。标记Shipment中的所有节点和标记都不会被创建。

非常感谢任何指向理由的指针。

1 个答案:

答案 0 :(得分:1)

对于正式错误,Visual Studio说,无法创建具有本地名称“xmlns”和空名称空间URI的属性。这是有道理的 - 你不会创建像这样的命名空间。

即使您可以为输出动态生成命名空间,它仍然无法正常工作,因为您的XSL模板无法查询数据文件。在您的数据中,您有<ActualShipment xmlns="1000181ZZZ">,因此ActualShipment下的所有节点都具有命名空间1000181ZZZ,您必须将该命名空间放入XSL中。否则,XSL会查找具有空白名称空间的节点,但找不到任何节点 - 所有节点都具有1000181ZZZ的名称空间。要从XSL中查询,您需要在xmlns:something="1000181ZZZ"的声明中添加<xsl:stylesheet>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:something="1000181ZZZ">

然后在xPath中使用它,如下所示:

<Shipment>
  <xsl:copy-of select="something:ActualShipment/something:Shipment/something:ShipmentHeader" />
  <xsl:for-each select="something:ActualShipment/something:Shipment/something:TransOrder" >
    <TransOrder>
      <xsl:copy-of select="something:TransOrderHeader" />
    </TransOrder>
  </xsl:for-each>
</Shipment>

您不应该使用变量等名称空间。它们应该提前知道。

一个明显的解决方法是将<ActualShipment xmlns="1000181ZZZ">替换为<ActualShipment some_attribute="1000181ZZZ">