首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问XSL中的特定节点

如何访问XSL中的特定节点
EN

Stack Overflow用户
提问于 2016-11-30 17:36:33
回答 1查看 786关注 0票数 0

愚蠢的问题,但希望我能在这里得到一些帮助,我需要访问XSL中的特定节点,我得到的XML如下所示,有人能告诉我我的XSL应该是什么样子才能访问节点“值”的内容--无论我如何尝试访问该节点!-任何帮助都可以!

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-30 18:36:04

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

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

XSL1.0

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

将返回:

结果

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<DR>-25043</DR>

注意事项:如果使用XSLT2.0处理器,可以声明xpath-default-namespace属性并取消前缀。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40894749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档