Elavarasan
Elavarasan

Reputation: 301

XSLT output value-of select is incorrect

My template_1.xml file

<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<TableDefinitions>
<Property Name="Category">\Table Definitions\Teradata\XML_TEST</Property>
   <Property Name="ShortDesc">Imported from: SRC_COLUMN_ADD_TEST</Property>
     <Collection Name="Columns" Type="MetaColumn">
        <SubRecord>
           <Property Name="Name">CUST_ID_1</Property>
           <Property Name="Description">CUST_ID: nullable int32</Property>
           <Property Name="SqlType">4</Property>
           <Property Name="Precision">9</Property>
           <Property Name="Scale">0</Property>
           <Property Name="Nullable">1</Property>
        </SubRecord>
        <SubRecord>
           <Property Name="Name">DESCR</Property>
           <Property Name="Description">DESCR: nullable string[max=144]</Property>
           <Property Name="SqlType">12</Property>
           <Property Name="Precision">144</Property>
           <Property Name="Scale">0</Property>
           <Property Name="Nullable">1</Property>
         </SubRecord>
        <SubRecord>
           <Property Name="Name">CUST_ADDR</Property>
           <Property Name="Description">CUST_ADDR: string[max=500]</Property>
           <Property Name="SqlType">12</Property>
           <Property Name="Precision">500</Property>
           <Property Name="Scale">0</Property>
           <Property Name="Nullable">0</Property>
        </SubRecord>
        <SubRecord>
           <Property Name="Name">AGE</Property>
           <Property Name="Description">AGE: nullable int32</Property>
           <Property Name="SqlType">4</Property>
           <Property Name="Precision">9</Property>
           <Property Name="Scale">0</Property>
           <Property Name="Nullable">1</Property>
        </SubRecord>
     </Collection>

Hi I am new to XSLT , I tried lot to get my expected out as mentioned below, some how am getting result only from same attribute value of same element, please help on this...Thanks

My template.xsl file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
    <DSExport>
        <id-of>
            <xsl:for-each select="//SubRecord">
                <SubRecord>
                <xsl:value-of select="//SubRecord/Property[@Name]" />
                </SubRecord>
            </xsl:for-each>
        </id-of>
    </DSExport>
</xsl:template>
</xsl:stylesheet>

My current ouput: output.xml

<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<id-of>
    <SubRecord>CUST_ID_1</SubRecord>
    <SubRecord>CUST_ID_1</SubRecord>
    <SubRecord>CUST_ID_1</SubRecord>
    <SubRecord>CUST_ID_1</SubRecord>
</id-of>
</DSExport>

My expected output :

<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<id-of>
    <SubRecord>CUST_ID_1</SubRecord>
    <SubRecord>DESCR</SubRecord>
    <SubRecord>CUST_ADDR</SubRecord>
    <SubRecord>AGE</SubRecord>
</id-of>
</DSExport>

Upvotes: 0

Views: 143

Answers (1)

Tim C
Tim C

Reputation: 70638

Your problem is with this expression

<xsl:value-of select="//SubRecord/Property[@Name]" />

The first slash at the start of the xpath expression indicates it is an absolute path, and so it will start searching from the document element. Two slashes then indicate it will search for the SubRecord element at any level in the document. This results in it always finding the first SubRecord element in the XML, regardless of where you are currently positioned.

You need to use a relative expression here. It will be relative to the context node you are currently positioned on (which is a SubRecord element). Try replacing it with this

<xsl:value-of select="Property[@Name]" />

Note that, strictly speaking this will get the first Property element which has an attribute of Name present. Although this gives your expected result, maybe it would be better written as this

<xsl:value-of select="Property[@Name='Name']" />

i.e. Get the Property which has a Name attribute with a value of Name.

Upvotes: 1

Related Questions