2014-01-24 92 views
0

我template_1.xml文件XSLT輸出的值不正確

<?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> 

您好我是新來的XSLT,我試過很多讓我的預期瞭如下文所述,一些如何我越來越只能從同一元素的同一屬性值的結果,請在此幫助...謝謝

我template.xsl文件

<?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> 

我現在的輸出中:與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> 

我的預期輸出:

<?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> 

回答

1

你的問題是與此表達

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

在第一個斜槓xpath表達式的開始表明它是一個n絕對路徑,所以它將從文檔元素開始搜索。兩個斜槓表示它將在文檔中的任何級別搜索元素SubRecord元素。這導致它始終在XML中找到第一個元素,不管您當前位於何處。

您需要在這裏使用相對錶達式。它將相對於您當前所在的上下文節點(這是一個SubRecord元素)。嘗試用這種

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

需要注意的是,嚴格的說這將讓其中有名稱存在的屬性中的第物業元素替換它。雖然這給您預期的結果,也許會更好的寫法,因爲這

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

即獲取物業具有名稱名稱值屬性。

+0

非常感謝您的快速響應,這很好的工作... – Elavarasan

+0

@ ela0250:如果這個答案解決了您的問題,請接受它作爲答案(標記在左邊的勾號)。 –