XPath检索递归节点

时间:2014-08-14 06:55:30

标签: xml xpath

我能否知道如何从以下xml中检索产品代码[@ Schema =“GTIN”]语句,NameLookups和NameTextItems值

<bb>
<Identity>
      <ProductCodes>
            <Code Scheme="GTIN">0218530000005</Code>
            <Code Scheme="BB">1392865</Code>
        </ProductCodes>
    </Identity>
    <Data>
      <ItemTypeGroup Id="0" Name="All Attributes">
                <Statements Id="1" Name="Third Party Logos">
                    <Statement Id="4">Union Flag</Statement>
                </Statements>
                <Statements Id="2" Name="Lifestyle">
                    <Statement Id="67">Organic</Statement>
                </Statements>
              <NameLookups Id="5" Name="Additives">
                    <NameValue>
                        <Name Id="137">Genetically Modified Ingredients</Name>
                        <Value Id="6">Free From</Value>
                    </NameValue>
                </NameLookups>
                <NameTextLookups Id="7" Name="Cooking Guidelines">
                    <NameValueText>
                        <Name Id="145">Oven cook</Name> 
                        <Value Id="10773">From Chilled</Value>
                        <Text xml:space="preserve">These</Text>
                    </NameValueText>
                </NameTextLookups>
                <Statements Id="10" Name="Storage and Usage Statements">
                    <Statement Id="174">Suitable for Home Freezing</Statement>
                    <Statement Id="205">Keep Refrigerated</Statement>
                </Statements>
                <NameLookups Id="11" Name="Recycling Info">
                    <NameValue>
                        <Name Id="199">Film</Name>
                        <Value Id="10">Not Recyclable</Value>
                    </NameValue>
                </NameLookups>
                <NameTextItems Id="16" Name="Dimension">
                    <NameText>
                        <Name Id="183">Shelf Height</Name>
                        <Text xml:space="preserve">154</Text>
                    </NameText>
                    <NameText>
                        <Name Id="184">Shelf Width</Name>
                        <Text xml:space="preserve">196</Text>
                    </NameText>
                    <NameText>
                        <Name Id="185">Shelf Depth</Name>
                        <Text xml:space="preserve">55</Text>
                    </NameText>
                </NameTextItems>
      <NameLookups Id="23" Name="Country">
                    <NameValue>
                        <Name Id="195">Country of Origin</Name>
                        <Value Id="251" Code="GB">United Kingdom</Value>
                    </NameValue>
                    <NameValue>
                        <Name Id="196">Packed In</Name>
                        <Value Id="251" Code="GB">United Kingdom</Value>
                    </NameValue>
                </NameLookups>
                <NameLookups Id="27" Name="Storage Type">
                    <NameValue>
                        <Name Id="190">Type</Name>
                        <Value Id="272">Chilled</Value>
                    </NameValue>
                </NameLookups>
                <NameTextItems Id="28" Name="Storage Conditions">
                    <NameText>
                        <Name Id="191">Min Temp C</Name>
                        <Text xml:space="preserve">-2</Text>
                    </NameText>
                    <NameText>
                        <Name Id="192">Max Temp C</Name>
                        <Text xml:space="preserve">4</Text>
                    </NameText>
                </NameTextItems>
                <NameLookups Id="29" Name="Pack Type">
                    <NameValue>
                        <Name Id="197">Type</Name>
                        <Value Id="326">Tray &amp; Heat Sealed</Value>
                    </NameValue>
                </NameLookups>
           <NameLookups Id="33" Name="Standardised Brand">
                    <NameValue>
                        <Name Id="224">Brand</Name>
                        <Value Id="572">Asda</Value>
                    </NameValue>
                </NameLookups>
         <NameTextItems Id="134" Name="Unit Merchandising">
                    <NameText>
                        <Name Id="1778">Height</Name>
                        <Text xml:space="preserve">55</Text>
                    </NameText>
                    <NameText>
                        <Name Id="1779">Width</Name>
                        <Text xml:space="preserve">196</Text>
                    </NameText>
                    <NameText>
                        <Name Id="1780">Depth</Name>
                        <Text xml:space="preserve">154</Text>
                    </NameText>
                </NameTextItems>
            </ItemTypeGroup>
        </Data>
</bb>

输出如下

Product Id (/bb/Identity/ProductCodes/Code[@Scheme="+"\"GTIN\""+"]/text()): 0218530000005
Statements Id : 1
Statement Id : 4
Statements Id : 2
Statement Id : 67
NameLookups Id :5
Name Id : 137
NameLookups Id :11
Name Id : 199
NameLookups Id :23
Name Id : 195
NameLookups Id :23
Name Id : 196
NameLookups Id :27
Name Id : 190
NameLookups Id :29
Name Id : 197
NameLookups Id :33
Name Id : 224

如何检索语句的所有XPath,NameLookups?

由于

1 个答案:

答案 0 :(得分:0)

以下是产生预期输出的XSLT 2.0样式表。您可以在线试用here

<强>样式表

模板不包含NameTextItems的ID,因为您只在问题文本中提及它们,但它们不会出现在预期的输出中。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" encoding="UTF-8" />

    <xsl:template match="/">
        <xsl:text>Product ID: </xsl:text>
        <xsl:value-of select="//Code[@Scheme = 'GTIN']"/>
        <xsl:text>&#x0a;</xsl:text>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="Statements | Statement | Name | NameLookups">
        <xsl:value-of select="local-name()"/>
        <xsl:text> ID: </xsl:text>
        <xsl:value-of select="@Id"/>
        <xsl:text>&#x0a;</xsl:text>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:transform>

<强>输出

Product ID: 0218530000005
Statements ID: 1
Statement ID: 4
Statements ID: 2
Statement ID: 67
NameLookups ID: 5
Name ID: 137
Name ID: 145
Statements ID: 10
Statement ID: 174
Statement ID: 205
NameLookups ID: 11
Name ID: 199
Name ID: 183
Name ID: 184
Name ID: 185
NameLookups ID: 23
Name ID: 195
Name ID: 196
NameLookups ID: 27
Name ID: 190
Name ID: 191
Name ID: 192
NameLookups ID: 29
Name ID: 197
NameLookups ID: 33
Name ID: 224
Name ID: 1778
Name ID: 1779
Name ID: 1780