开发者

Want to write XSLT for a XML to get a tree structure?

开发者 https://www.devze.com 2023-03-21 07:33 出处:网络
I have got some XML like this; I want to be writing a XSLT. Where I can extract the attribute V. And produce a Tree Structure like this.

I have got some XML like this; I want to be writing a XSLT. Where I can extract the attribute V. And produce a Tree Structure like this.

PS
.
.
....Product Category
.           .
.           .
.           .
.            Product.
.
....Financial Product Images
           .
           .
           .Product2.

Other
.
.
........Customer Location Images
              .
              .
              . Service3.

  <PV V="PS:Product Category:Product1" L="" H="" C="327" /> 
  <PV V="PS:Financial Product Images:Product2" L="" H="" C="173" /> 
  <PV V="Other:Customer Location Images:Service2" L="" H="" C="122" /> 
  <PV V="PS:POS Product Images:Product3" L="" H="" C="109" /> 
  <PV V="N/A" L="" H="" C="106" /> 
  <PV V="Other:Customer Location Images:Service 3" L="" H="" C="98" /> 


Can anybody please hel开发者_StackOverflowp me i am very new to XSLT


You could use this XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/wrapper">
        <wrapper2>
            <xsl:apply-templates select="PV[starts-with(@V,'PS')]"/>
            <xsl:apply-templates select="PV[starts-with(@V,'Other')]"/>
        </wrapper2>
    </xsl:template>

    <xsl:template match="//PV">
        <xsl:variable name="elementName1" select="substring-before(./@V,':')"/>
        <xsl:variable name="elementName23" select="substring-after(./@V,':')"/>
        <xsl:variable name="elementName2"
            select="translate(substring-before($elementName23,':'), ' ', '_')"/>
        <xsl:variable name="elementName3"
            select="translate(substring-after($elementName23,':'), ' ', '_')"/>

        <xsl:if test="not($elementName1 = '')">
            <xsl:element name="{$elementName1}">
                <xsl:element name="{$elementName2}">
                    <xsl:element name="{$elementName3}"> </xsl:element>
                </xsl:element>
            </xsl:element>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

A tag name cannot have whitespaces so you need to replace them by some other character. This is done here by replacing spaces by '_'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号