如何访问XSL中的特定节点

时间:2016-11-30 17:36:33

标签: xslt

愚蠢的问题,但希望我能在这里得到一些帮助,我需要访问XSL中的特定节点,我提供的XML如下所示,有人可以让我知道我的XSL应该是什么样子才能访问节点'价值'的内容我无论如何尝试到达那个节点我都不能! - 任何帮助都赞赏!!!! :

<?xml version="1.0" encoding="UTF-8" ?>
<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
<Group Level="1">
    <Group Level="2">
        <Details Level="3">
            <Section SectionNumber="1">
                <Field Name="Field5" FieldName="{ARTransaction.Transactions}">
                    <FormattedValue>0.00</FormattedValue>
                    <Value>0.00</Value>
                </Field>
                <Field Name="Field15" FieldName="{ARTransaction.PostingDate}">
                    <FormattedValue>8/1/2016</FormattedValue>
                    <Value>2016-08-01</Value>
                </Field>
                <Field Name="Field14" FieldName="{ARTransaction.AuditTrail}">
                    <FormattedValue>2016083100154</FormattedValue>
                    <Value>2016083100154</Value>
                </Field>
                <Field Name="Field13" FieldName="{ARTransaction.JobN}">
                    <FormattedValue>-25043</FormattedValue>
                    <Value>-25043</Value>
                </Field>
                <Field Name="Field11" FieldName="{Customer.CustomerName}">
                    <FormattedValue>First Church of Christ</FormattedValue>
                    <Value>First Church of Christ</Value>
                </Field>
                <Field Name="Field7" FieldName="{ARTransaction.CustomerN}">
                    <FormattedValue>13157</FormattedValue>
                    <Value>13157</Value>
                </Field>
                <Field Name="Field6" FieldName="{ARTransaction.InvoiceN}">
                    <FormattedValue>25043</FormattedValue>
                    <Value>25043</Value>
                </Field>
                <Field Name="SalesmanN1" FieldName="{ARTransaction.SalesmanN}">
                    <FormattedValue>22</FormattedValue>
                    <Value>22</Value>
                </Field>
            </Section>
        </Details>
    </Group>
    </Group>
</Group>
<ReportFooter> </ReportFooter>

XSL:

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/"> 
    <xsl:element name="CrystalReport">   
        <xsl:attribute name="xsi:schemaLocation">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
        <xsl:attribute name="version">1.2</xsl:attribute>     
    </xsl:element> 
    <xsl:element name="DR">
    <xsl:value-of select="Group/Group/Details/Section/Field[@Name='Field13']/Value"/>
    </xsl:element>

</xsl:template>

1 个答案:

答案 0 :(得分:0)

您尝试的主要问题是它忽略了源XML的默认命名空间

您需要在样式表中声明命名空间,为其指定前缀,并在寻址源XML中的元素时使用该前缀。例如,以下样式表:

XSL 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="urn:crystal-reports:schemas:report-detail"
exclude-result-prefixes="ns1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/ns1:CrystalReport">
    <DR>
        <xsl:value-of select="ns1:Group/ns1:Group/ns1:Details/ns1:Section/ns1:Field[@Name='Field13']/ns1:Value"/>
    </DR>
</xsl:template>

</xsl:stylesheet>

应用于格式正确的输入,例如:

<强> XML

<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
  <Group Level="1">
    <Group Level="2">
      <Details Level="3">
        <Section SectionNumber="1">
          <Field Name="Field5" FieldName="{ARTransaction.Transactions}">
            <FormattedValue>0.00</FormattedValue>
            <Value>0.00</Value>
          </Field>
          <Field Name="Field15" FieldName="{ARTransaction.PostingDate}">
            <FormattedValue>8/1/2016</FormattedValue>
            <Value>2016-08-01</Value>
          </Field>
          <Field Name="Field14" FieldName="{ARTransaction.AuditTrail}">
            <FormattedValue>2016083100154</FormattedValue>
            <Value>2016083100154</Value>
          </Field>
          <Field Name="Field13" FieldName="{ARTransaction.JobN}">
            <FormattedValue>-25043</FormattedValue>
            <Value>-25043</Value>
          </Field>
          <Field Name="Field11" FieldName="{Customer.CustomerName}">
            <FormattedValue>First Church of Christ</FormattedValue>
            <Value>First Church of Christ</Value>
          </Field>
          <Field Name="Field7" FieldName="{ARTransaction.CustomerN}">
            <FormattedValue>13157</FormattedValue>
            <Value>13157</Value>
          </Field>
          <Field Name="Field6" FieldName="{ARTransaction.InvoiceN}">
            <FormattedValue>25043</FormattedValue>
            <Value>25043</Value>
          </Field>
          <Field Name="SalesmanN1" FieldName="{ARTransaction.SalesmanN}">
            <FormattedValue>22</FormattedValue>
            <Value>22</Value>
          </Field>
        </Section>
      </Details>
    </Group>
  </Group>
</CrystalReport>

将返回:

<强>结果

<?xml version="1.0" encoding="UTF-8"?>
<DR>-25043</DR>

注意:如果您使用的是XSLT 2.0处理器,则可以声明xpath-default-namespace属性并取消前缀。