2011-08-30 81 views
1

我有下面的XML計劃,我想選擇Table>Row>Field with name = AmountXSL:選擇元素值使用XPath選擇

<?xml version="1.0" encoding="utf-8"?> 
<Document> 
    <PermitDocument code="01312" id="1"> 
    <Field name="DocumentType">Certificate</Field> 
    ... 
    ... 
    <Table name="Products"> 
     <Row> 
     <Field name="Amount">1000</Field> 
     ... 
     ... 
     </Row> 
     <Row> 
     <Field name="Amount">3000</Field> 
     ... 
     ... 
     </Row> 
    </Table> 
    </PermitDocument> 
</Document> 

我爲轉型,但沒有下面的代碼發生

<xsl:for-each select="Document/PermitDocument/Table[@name='Products']/Row"> 
    <xsl:value-of select="Field[@name='Amount']" /> 
    ... 
    ... 
</xsl:for-each> 

C#代碼

XslTransform xs = new XslTransform(); 

try 
{ 
    xs.Load("BCIS.xsl"); 
} 
catch (XsltException e) 
{ 
    Console.WriteLine(e.StackTrace); 
    Console.ReadLine(); 
} 

xs.Transform("BCIS.xml", "BCIS.html"); 

我用來實現目標的語法有什麼問題?

蘇丹

回答

1

輸入:

<Document> 
    <PermitDocument code="01312" id="1"> 
     <Field name="DocumentType">Certificate</Field> 
     <Table name="Products"> 
      <Row> 
       <Field name="Amount">1000</Field> 
      </Row> 
      <Row> 
       <Field name="Amount">3000</Field> 
      </Row> 
     </Table> 
    </PermitDocument> 
</Document> 

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
     <xsl:for-each select="Document/PermitDocument/Table[@name='Products']/Row"> 
      <xsl:value-of select="Field[@name='Amount']" /> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

輸出:

10003000 
+0

我使用HTML模板而不是XML和當我設置'的',輸出丟失 – sultan

+0

@sultan,什麼是「HTML模板」? –

+0

對不起,XSLT模板結合html – sultan