1

I have an xml an example at the bottom, where there are several <offer> elements each with child sku element. I want to extract only the text node of the element <sku>

I tried the following XPATH in an XSLT but none gets me just 390500 as the output. All of them end up reading every text node in the file.

<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

<?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>
1
  • It doesn't make much sense to show a complex XML with elements in a namespace and then a snippet of XSLT without any namespace context. Do you use XSLT 2 or 3 and have set up the xpath-default-namespace? Or how do you expect that templates to match at all. And if there "are several elements each with child sku", of which one do you want to extract the contents of the sku elements? What is the result you want, which output type (text, xml, (x)html) do you want to create with XSLT? Commented Sep 25, 2018 at 13:01

2 Answers 2

1

The XPath expression //offer/sku/text() selects the text of all sku child elements of all offer elements.

> xmllint --xpath '//offer/sku/text()' ~/net/test.xml
390500

Note that your original document contains a default namespace, you need to handle that in an appropriate manner, depending on the tool you are using. Otherwise the selected XPath set will be empty.

Sign up to request clarification or add additional context in comments.

Comments

0

<xsl:value-of select="/enfinity/offer[1]/sku[1]"/> should do the trick.

Note that the entire document will still be tokenized and put in RAM if you use XSLT.

1 Comment

"Note that the entire document will still be tokenized and put in RAM if you use XSLT": in XSLT 3 with streaming you could avoid that, for instance by using xsl:iterate and xsl:break as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.