首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT/XPATH仅提取一个文本节点

使用XSLT/XPATH仅提取一个文本节点
EN

Stack Overflow用户
提问于 2018-09-25 20:06:24
回答 2查看 521关注 0票数 1

我在底部有一个xml示例,其中有几个<offer>元素,每个元素都有sku子元素。我只想提取元素<sku>的文本节点

我在XSLT中尝试了下面的XPATH,但都没有得到390500作为输出。所有这些节点最终都会读取文件中的每个文本节点。

代码语言:javascript
复制
<xsl:template match="offer[sku/text()]">
    <xsl:value-of select="./text()"/>
</xsl:template>

<xsl:template match="offer/sku">
 <xsl:for-each select="sku">
    <xsl:value-of select="sku"/>
 </xsl:for-each>
</xsl:template>

XML

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<enfinity xmlns='http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xml='http://www.w3.org/XML/1998/namespace' xmlns:dt='http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt' xsi:schemaLocation="http://www.intershop.com/xml/ns/enfinity/7.1/xcs/impex catalog.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" major="6" minor="1" family="enfinity" branch="enterprise" build="build" >
    <offer sku="390500" >
        <available>1</available>
        <online>1</online>
        <product-type name="Basic" domain="system" ></product-type>
        <sku>390500</sku>
        <tax-class id="FullTax" ></tax-class>
        <supplier>
          <supplier-name>1573</supplier-name>
          <supplier-sku/>
        </supplier>
        <quantity unit="Piece" >
          <step-quantity>1</step-quantity>
          <price-quantity>1</price-quantity>
        </quantity>
        <custom-attributes>
          <custom-attribute name="Country" dt:dt="string" >
            <value>US</value>
          </custom-attribute>
          <custom-attribute name="LICENSED" dt:dt="string" >
            <value>Y</value>
          </custom-attribute>
          <custom-attribute name="LeadTime" dt:dt="string" >
            <value>001</value>
          </custom-attribute>
          <custom-attribute name="LifeCycle" dt:dt="string" >
            <value>03</value>
          </custom-attribute>
          <custom-attribute name="SalesOrganization" dt:dt="string" >
            <value>1573</value>
          </custom-attribute>
        </custom-attributes>
        <offered-product sku="390500" domain="LBS-LBSUS" ></offered-product>
        <type-code>258</type-code>
    </offer>
</enfinity>
EN

回答 2

Stack Overflow用户

发布于 2018-09-25 21:18:17

XPath表达式//offer/sku/text()选择所有sku元素的所有offer子元素的文本。

代码语言:javascript
复制
> xmllint --xpath '//offer/sku/text()' ~/net/test.xml
390500

请注意,原始文档包含一个默认名称空间,您需要根据所使用的工具以适当的方式处理该名称空间。否则,所选的XPath集合将为空。

票数 1
EN

Stack Overflow用户

发布于 2018-09-25 21:08:02

<xsl:value-of select="/enfinity/offer[1]/sku[1]"/>应该可以做到这一点。

请注意,如果使用XSLT,整个文档仍将被标记化并放入RAM中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52497856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档