0
<Products xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" 
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

<Product>
<status>1</status> 
<ProductName>******************</ProductName> 
<model>******************</model>
<ProductQTY>******************</ProductQTY> 
<CostPrice>******************</CostPrice>
<RetailPrice>******************</RetailPrice> 
<FullCategoryPath>******************</FullCategoryPath>
<description> 
<![CDATA[******************
]]></description> 
<image_link>https://******************</image_link> 
</Product>
<Product>
<status>1</status>
<ProductName>******************</ProductName>
<model>******************7</model>
<ProductQTY>0</ProductQTY> 
<CostPrice>1******************</CostPrice>
<RetailPrice>141.9</RetailPrice> 
<FullCategoryPath>******************</FullCategoryPath><description> 
<![CDATA******************
  ]]></description> 
<image_link>https://******************</image_link>
</Product>
</Products>    <!-- Added by edit -->

as you see above.
How can I fix my XSLT file?

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
            xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9" 
            xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"               
            exclude-result-prefixes="sitemap image">

    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node() "/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*">
        <xml>
            <xsl:for-each select="node()">
                <xsl:choose >
                    <xsl:when test="node() | model !=''">
                        <Product>     
                            <xsl:apply-templates select="@* | node() "/>
                            <SProductCode >
                                <xsl:value-of select="node() | @model" />
                            </SProductCode>
                            <CategoryName>
                                <value-of select=""/>
                            </CategoryName>
                            .
                            .
                            .
                            .
                        </Product>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xml>
    </xsl:template>
</xsl:stylesheet>    <!-- Added by edit -->

For example:
How can I show category names product name ex. from my node(). I can't parse them.

xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"

These two namespaces ruin me so badly.They are inside of my root element"Products".

Output is: All values are false .

<xml>
     <Product>
       <SProductCode>1</SProductCode>
       <CategoryName>
         <value-of select="node() | @model" />
       </CategoryName>
     </Product>
     <Product>
       <SProductCode>1</SProductCode>
       <CategoryName>
         <value-of select="node() | @model" />
       </CategoryName>
     </Product>

What am I gonna write for "SProductCode" or"CategoryName" to properly run it?

7
  • In the result do appear only SProductCode> and CategoryName elements with values beside empty <xml/> nodes. Is it possible that you gave a partial output as input XML in your question? Commented Sep 24, 2018 at 13:27
  • The namespaces listed in the XML are not actually used anywhere (all the elements in your XML are not in any namespaces) so they shouldn't actually cause a problem in this instance. Commented Sep 24, 2018 at 13:34
  • @zx485 Output added Commented Sep 24, 2018 at 14:29
  • @timc if ı write a normal xslt , Im getting null xml because sitemap and image block to list it . Commented Sep 24, 2018 at 14:41
  • I still don't understand what you're trying to achieve. Is this the desired output? When I apply the stylesheet, I get a different output. And in what way is that problem related to the namespaces, because, as @TimC wrote, they are unused in the XML? Commented Sep 24, 2018 at 14:48

1 Answer 1

0

The following modified template approaches your desired output. I removed some unnecessary code, but I couldn't derive which values you want to put into the xsl:value-of select="..." />. I also changed your xsl:choose to a simple predicate of the Product xsl:for-each loop.

<xsl:template match="/Products">
    <xml>
        <xsl:for-each select="Product[model != '']">
            <Product>     
                <SProductCode >
                    <xsl:value-of select="model" />
                </SProductCode>
                <CategoryName>
                    <xsl:value-of select="FullCategoryPath"/>
                </CategoryName>
                .
                .
                .
                .
            </Product>
        </xsl:for-each>
    </xml>
</xsl:template>

Output is:

<xml>
    <Product>
        <SProductCode>******************</SProductCode>
        <CategoryName>******************</CategoryName>
        .
        .
        .
        .
    </Product>
    <Product>
        <SProductCode>******************7</SProductCode>
        <CategoryName>******************</CategoryName>
        .
        .
        .
        .
    </Product>
</xml>
Sign up to request clarification or add additional context in comments.

3 Comments

ı put breakpoint to check then it's not enter the <xsl:template match="/Products">. So they are not getting my Product attributes like SProductCode,CategoryName...
Of course you can (and must) adjust the match rule to the actual design of your full XML. It could be simply Products instead, for example.
I tried almost all posibilities and again null.I can't take right values from xml and parse them in my xml attributes.

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.