XSL:使用XPath选择器选择元素值

时间:2011-08-30 10:09:43

标签: xml xslt xpath css-selectors transformation

我有以下XML方案,我想选择Table>Row>Field with name = Amount

<?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 个答案:

答案 0 :(得分: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