xpath: 选择第 n 项返回全部

发布于 2024-10-15 07:28:21 字数 8402 浏览 2 评论 0原文

当我根据下面的 xslt 使用 xpath 选择


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS"
exclude-result-prefixes="ns">
    <xsl:template match="/">
    <ManifestSourceM>
        <xsl:for-each select="ns:Shipment/ns:ShipmentInformation">
            <DSManifest>
                <ManifestNo>
                    <xsl:value-of select="ns:GeneralShipmentInformation/ns:ShipmentIdentificationNumber"></xsl:value-of>
                </ManifestNo>
                <ManifestDate>
                    <xsl:value-of select="ns:DateTimeReference/ns:Date"></xsl:value-of>
                </ManifestDate>
                <PrimeMover>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TV'][1]/ns:EquipmentStructure/ns:EquipmentNumber"></xsl:value-of>
                </PrimeMover>
                <Fleet1>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][1]/ns:EquipmentStructure/ns:EquipmentNumber[1]"></xsl:value-of>
                </Fleet1>
                <Fleet2>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][2]/ns:EquipmentStructure/ns:EquipmentNumber[2]"></xsl:value-of>
                </Fleet2>
                <Fleet3>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][3]/ns:EquipmentStructure/ns:EquipmentNumber[3]"></xsl:value-of>
                </Fleet3>
                <Fleet4>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][4]/ns:EquipmentStructure/ns:EquipmentNumber[4]"></xsl:value-of>
                </Fleet4>
                <Fleet5>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][5]/ns:EquipmentStructure/ns:EquipmentNumber[5]"></xsl:value-of>
                </Fleet5>
                <RouteCode>
                    <xsl:value-of select="ns:GeneralShipmentInformation/ns:StandardCarrierAlphaCode"></xsl:value-of>
                </RouteCode>
                <Description><xsl:value-of select="string-join(('BlueScope Load:', ns:GeneralShipmentInformation/ns:ShipmentIdentificationNumber, ' On RunCode:', ns:GeneralShipmentInformation/ns:StandardCarrierAlphaCode), '')"></xsl:value-of>
                </Description>
                <Description2></Description2>
            </DSManifest>
           </xsl:for-each>
        </ManifestSourceM>
    </xsl:template>
</xsl:stylesheet>

并将其应用于此 xml 时,


<?xml version="1.0" encoding="UTF-8"?>
<ns:Shipment xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS">
    <ns:ShipmentInformation Transaction="Actual">
        <ns:GeneralShipmentInformation>
            <ns:TransactionSetPurposeCode>00</ns:TransactionSetPurposeCode>
            <ns:TransportationMethodTypeCode>L</ns:TransportationMethodTypeCode>
            <ns:ShipmentMethodOfPayment>DE</ns:ShipmentMethodOfPayment>
            <ns:ShipmentIdentificationNumber>178896</ns:ShipmentIdentificationNumber>
            <ns:StandardCarrierAlphaCode>NUNCOL</ns:StandardCarrierAlphaCode>
        </ns:GeneralShipmentInformation>
        <ns:Priority>0</ns:Priority>
        <ns:DateTimeReference SegmentId="StartTime">
            <ns:Date>2010-12-09</ns:Date>
            <ns:Time>07:00:00</ns:Time>
        </ns:DateTimeReference>
        <ns:Origin>
            <ns:GeographicLocation>
                <ns:LocationIdentifier>NUN</ns:LocationIdentifier>
            </ns:GeographicLocation>
        </ns:Origin>
        <ns:Destination>
            <ns:GeographicLocation>
                <ns:LocationIdentifier>~COMPLETE STEEL ROOFING</ns:LocationIdentifier>
            </ns:GeographicLocation>
        </ns:Destination>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>Z26GW</ns:EquipmentNumber>
                    <ns:DescriptionCode>TV</ns:DescriptionCode>
                </ns:EquipmentStructure>
            </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-5</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
            </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-4</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-2</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-3</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-1</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
    </ns:ShipmentInformation>
</ns:Shipment>

我得到了这个。 Fleet1 元素包含所有发生的事件。


<?xml version="1.0" encoding="UTF-8"?>
<ManifestSourceM>
    <DSManifest>
        <ManifestNo>178896</ManifestNo>
        <ManifestDate>2010-12-09</ManifestDate>
        <PrimeMover>Z26GW</PrimeMover>
        <Fleet1>B16KY-5 B16KY-4 B16KY-2 B16KY-3 B16KY-1</Fleet1>
        <Fleet2/>
        <Fleet3/>
        <Fleet4/>
        <Fleet5/>
        <RouteCode>NUNCOL</RouteCode>
        <Description>BlueScope Load:178896 On RunCode:NUNCOL</Description>
        <Description2/>
    </DSManifest>
</ManifestSourceM>

我什至尝试使用 for-each-group 并选择...当特定于每次出现时,但仍然得到相同的结果。

我无法弄清楚如何在 select...when 方法中使用它。


<xsl:number count="EquipmentDetailsLoop[normalize-space(EquipmentDetails/EquipmentStructure/DescriptionCode)='TL']" level="single"></xsl:number>

When I use the xpath select as per the xslt below


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS"
exclude-result-prefixes="ns">
    <xsl:template match="/">
    <ManifestSourceM>
        <xsl:for-each select="ns:Shipment/ns:ShipmentInformation">
            <DSManifest>
                <ManifestNo>
                    <xsl:value-of select="ns:GeneralShipmentInformation/ns:ShipmentIdentificationNumber"></xsl:value-of>
                </ManifestNo>
                <ManifestDate>
                    <xsl:value-of select="ns:DateTimeReference/ns:Date"></xsl:value-of>
                </ManifestDate>
                <PrimeMover>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TV'][1]/ns:EquipmentStructure/ns:EquipmentNumber"></xsl:value-of>
                </PrimeMover>
                <Fleet1>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][1]/ns:EquipmentStructure/ns:EquipmentNumber[1]"></xsl:value-of>
                </Fleet1>
                <Fleet2>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][2]/ns:EquipmentStructure/ns:EquipmentNumber[2]"></xsl:value-of>
                </Fleet2>
                <Fleet3>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][3]/ns:EquipmentStructure/ns:EquipmentNumber[3]"></xsl:value-of>
                </Fleet3>
                <Fleet4>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][4]/ns:EquipmentStructure/ns:EquipmentNumber[4]"></xsl:value-of>
                </Fleet4>
                <Fleet5>
                    <xsl:value-of select="ns:EquipmentDetailsLoop/ns:EquipmentDetails[ns:EquipmentStructure/ns:DescriptionCode = 'TL'][5]/ns:EquipmentStructure/ns:EquipmentNumber[5]"></xsl:value-of>
                </Fleet5>
                <RouteCode>
                    <xsl:value-of select="ns:GeneralShipmentInformation/ns:StandardCarrierAlphaCode"></xsl:value-of>
                </RouteCode>
                <Description><xsl:value-of select="string-join(('BlueScope Load:', ns:GeneralShipmentInformation/ns:ShipmentIdentificationNumber, ' On RunCode:', ns:GeneralShipmentInformation/ns:StandardCarrierAlphaCode), '')"></xsl:value-of>
                </Description>
                <Description2></Description2>
            </DSManifest>
           </xsl:for-each>
        </ManifestSourceM>
    </xsl:template>
</xsl:stylesheet>

And apply it to this xml


<?xml version="1.0" encoding="UTF-8"?>
<ns:Shipment xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS">
    <ns:ShipmentInformation Transaction="Actual">
        <ns:GeneralShipmentInformation>
            <ns:TransactionSetPurposeCode>00</ns:TransactionSetPurposeCode>
            <ns:TransportationMethodTypeCode>L</ns:TransportationMethodTypeCode>
            <ns:ShipmentMethodOfPayment>DE</ns:ShipmentMethodOfPayment>
            <ns:ShipmentIdentificationNumber>178896</ns:ShipmentIdentificationNumber>
            <ns:StandardCarrierAlphaCode>NUNCOL</ns:StandardCarrierAlphaCode>
        </ns:GeneralShipmentInformation>
        <ns:Priority>0</ns:Priority>
        <ns:DateTimeReference SegmentId="StartTime">
            <ns:Date>2010-12-09</ns:Date>
            <ns:Time>07:00:00</ns:Time>
        </ns:DateTimeReference>
        <ns:Origin>
            <ns:GeographicLocation>
                <ns:LocationIdentifier>NUN</ns:LocationIdentifier>
            </ns:GeographicLocation>
        </ns:Origin>
        <ns:Destination>
            <ns:GeographicLocation>
                <ns:LocationIdentifier>~COMPLETE STEEL ROOFING</ns:LocationIdentifier>
            </ns:GeographicLocation>
        </ns:Destination>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>Z26GW</ns:EquipmentNumber>
                    <ns:DescriptionCode>TV</ns:DescriptionCode>
                </ns:EquipmentStructure>
            </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-5</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
            </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-4</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-2</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-3</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
        <ns:EquipmentDetailsLoop>
            <ns:EquipmentDetails>
                <ns:EquipmentStructure>
                    <ns:Initial/>
                    <ns:EquipmentNumber>B16KY-1</ns:EquipmentNumber>
                    <ns:DescriptionCode>TL</ns:DescriptionCode>
                </ns:EquipmentStructure>
             </ns:EquipmentDetails>
        </ns:EquipmentDetailsLoop>
    </ns:ShipmentInformation>
</ns:Shipment>

I get this. The Fleet1 element contains all the occurences.


<?xml version="1.0" encoding="UTF-8"?>
<ManifestSourceM>
    <DSManifest>
        <ManifestNo>178896</ManifestNo>
        <ManifestDate>2010-12-09</ManifestDate>
        <PrimeMover>Z26GW</PrimeMover>
        <Fleet1>B16KY-5 B16KY-4 B16KY-2 B16KY-3 B16KY-1</Fleet1>
        <Fleet2/>
        <Fleet3/>
        <Fleet4/>
        <Fleet5/>
        <RouteCode>NUNCOL</RouteCode>
        <Description>BlueScope Load:178896 On RunCode:NUNCOL</Description>
        <Description2/>
    </DSManifest>
</ManifestSourceM>

I even tried with a for-each-group and choose...when specific to each occurence, but still got the same result.

I couldn't work out how to use this in the choose...when method.


<xsl:number count="EquipmentDetailsLoop[normalize-space(EquipmentDetails/EquipmentStructure/DescriptionCode)='TL']" level="single"></xsl:number>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2024-10-22 07:28:21

作为一个缩减示例,此样式表:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS"
 exclude-result-prefixes="ns">
    <xsl:strip-space elements="*">
    <xsl:template match="ns:Shipment">
        <ManifestSourceM>
            <xsl:apply-templates/>
        </ManifestSourceM>
    </xsl:template>
    <xsl:template match="ns:ShipmentInformation">
        <DSManifest>
            <xsl:apply-templates/>
        </DSManifest>
    </xsl:template>
    <xsl:template match="ns:ShipmentIdentificationNumber">
        <ManifestNo>
            <xsl:value-of select="."/>
        </ManifestNo>
    </xsl:template>
    <xsl:template match="ns:Date">
        <ManifestDate>
            <xsl:value-of select="."/>
        </ManifestDate>
    </xsl:template>
    <xsl:template match="ns:EquipmentStructure[ns:DescriptionCode='TV']">
        <PrimeMover>
            <xsl:value-of select="ns:EquipmentNumber"/>
        </PrimeMover>
    </xsl:template>
    <xsl:template match="ns:EquipmentStructure[ns:DescriptionCode='TL']">
        <xsl:variable name="vNumber">
            <xsl:number count="ns:EquipmentStructure[ns:DescriptionCode='TL']"
                        level="any"/>
        </xsl:variable>
        <xsl:element name="Fleet{$vNumber}">
            <xsl:value-of select="ns:EquipmentNumber"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

输出:

<ManifestSourceM>
    <DSManifest>
        <ManifestNo>178896</ManifestNo>
        <ManifestDate>2010-12-09</ManifestDate>
        <PrimeMover>Z26GW</PrimeMover>
        <Fleet1>B16KY-5</Fleet1>
        <Fleet2>B16KY-4</Fleet2>
        <Fleet3>B16KY-2</Fleet3>
        <Fleet4>B16KY-3</Fleet4>
        <Fleet5>B16KY-1</Fleet5>
    </DSManifest>
</ManifestSourceM>

注意:覆盖文本节点内置规则。模式匹配我们只想转换(更高层次结构)或转换并输出值(叶子)的任何元素。使用 xsl:element 指令的数据驱动元素名称。使用 xsl:number 指令进行层次结构编号。

As a reduce example, this stylesheet:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ns="urn:ROCK-BlueScope/ShipmentImportWS"
 exclude-result-prefixes="ns">
    <xsl:strip-space elements="*">
    <xsl:template match="ns:Shipment">
        <ManifestSourceM>
            <xsl:apply-templates/>
        </ManifestSourceM>
    </xsl:template>
    <xsl:template match="ns:ShipmentInformation">
        <DSManifest>
            <xsl:apply-templates/>
        </DSManifest>
    </xsl:template>
    <xsl:template match="ns:ShipmentIdentificationNumber">
        <ManifestNo>
            <xsl:value-of select="."/>
        </ManifestNo>
    </xsl:template>
    <xsl:template match="ns:Date">
        <ManifestDate>
            <xsl:value-of select="."/>
        </ManifestDate>
    </xsl:template>
    <xsl:template match="ns:EquipmentStructure[ns:DescriptionCode='TV']">
        <PrimeMover>
            <xsl:value-of select="ns:EquipmentNumber"/>
        </PrimeMover>
    </xsl:template>
    <xsl:template match="ns:EquipmentStructure[ns:DescriptionCode='TL']">
        <xsl:variable name="vNumber">
            <xsl:number count="ns:EquipmentStructure[ns:DescriptionCode='TL']"
                        level="any"/>
        </xsl:variable>
        <xsl:element name="Fleet{$vNumber}">
            <xsl:value-of select="ns:EquipmentNumber"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

Output:

<ManifestSourceM>
    <DSManifest>
        <ManifestNo>178896</ManifestNo>
        <ManifestDate>2010-12-09</ManifestDate>
        <PrimeMover>Z26GW</PrimeMover>
        <Fleet1>B16KY-5</Fleet1>
        <Fleet2>B16KY-4</Fleet2>
        <Fleet3>B16KY-2</Fleet3>
        <Fleet4>B16KY-3</Fleet4>
        <Fleet5>B16KY-1</Fleet5>
    </DSManifest>
</ManifestSourceM>

Note: Overwriting text node built-in rule. Pattern matching any element we want to only transform (higher hierarchy) or transform and output value (leafs). Data driven elements names with xsl:element instruction. Hierarchy numbering with xsl:number instruction.

猫瑾少女 2024-10-22 07:28:21

您的问题在这里:

ns:EquipmentDetailsLoop
 /ns:EquipmentDetails
  [ns:EquipmentStructure
        /ns:DescriptionCode = 'TL'
  ][1]
      /ns:EquipmentStructure
        /ns:EquipmentNumber[1]

这会从所有 ns:EquipmentDetailsLoop 子树中选择所需的ns:EquipmentNumber

由于您使用的是 XSLT 2.0, 指令会输出一个文本节点,其中包含由 select 选择的节点集中每个节点的字符串值代码>属性。这样就输出了所有 4 个字符串。

您想要

(/*/*/*/*/*[ns:DescriptionCode='TL']/ns:EquipmentNumber)[$k]

其中$k在范围内(必须被该范围内的每个整数替换一次):

1count ((/*/*/*/*/*[ns:DescriptionCode='TL']/ns:设备编号))

Your problem is here:

ns:EquipmentDetailsLoop
 /ns:EquipmentDetails
  [ns:EquipmentStructure
        /ns:DescriptionCode = 'TL'
  ][1]
      /ns:EquipmentStructure
        /ns:EquipmentNumber[1]

This selects the wanted ns:EquipmentNumber from all ns:EquipmentDetailsLoop subtrees.

Because you are using XSLT 2.0, the <xsl:value-of> instruction outputs one text node with the string value of every node in the node-set, selected by the select attribute. Thus all 4 strings are output.

You want:

(/*/*/*/*/*[ns:DescriptionCode='TL']/ns:EquipmentNumber)[$k]

where $k is in the range (must be substituted once by every integer in this range):

1 to count((/*/*/*/*/*[ns:DescriptionCode='TL']/ns:EquipmentNumber))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文