【问题标题】:How to get the main element value using xslt 1.0?如何使用 xslt 1.0 获取主元素值?
【发布时间】:2019-05-09 08:00:44
【问题描述】:

如何获取Header 主元素“AMEX,AMEX”的值? 下面是我的 xml,其中包含标题和值 AMEX,AMEX。我想获取 "AMEX,AMEX" 值以将其显示在 xsl:fo 表格单元格中。

xml

<Documents>
 <Document>
<PackingNote>
  <Header>AMEX, AMEX<CustomerDetail>Adeel/000004</CustomerDetail><Despatchdate>09/05/2019</Despatchdate><OrderNumber>000534</OrderNumber><OrderNumberBatch>000534-1</OrderNumberBatch></Header>
  <Header>AMEX, AMEX<CustomerDetail>Adeel/000004</CustomerDetail><Despatchdate>09/05/2019</Despatchdate><OrderNumber>000534</OrderNumber><OrderNumberBatch>000534-2</OrderNumberBatch></Header>
  <Lines>
    <Line>
      <ProductName>Kid-Sweater-Winter Aqua</ProductName>
      <Quantity>1.00</Quantity>
      <Price>£15.00</Price>
      <Size>Small/Slim Fit</Size>
      <ReasonCode />
      <ReturnQty />
      <Fit>Fit </Fit>
    </Line>
  </Lines>
  <PackedBy>
    <SubTotal>£30</SubTotal>
    <Shipping>£2.00</Shipping>
    <Total>£32</Total>
  </PackedBy>
</PackingNote>
  </Document>
</Documents>

xslt

<xsl:template match="Header">
<!-- Title -->
<fo:table table-width="27.5cm" table-height="2cm">
  <fo:table-column column-width="5.5cm" />
  <fo:table-column column-width="3.5cm" />
  <fo:table-column column-width="14.5cm" />
  <fo:table-column column-width="4cm" />
  <fo:table-body>
    <fo:table-row border-width="0mm">
      <fo:table-cell>
        <fo:table table-layout="fixed">
          <fo:table-column column-width="5cm" />
          <fo:table-body>
            <fo:table-row />
          </fo:table-body>
        </fo:table>
      </fo:table-cell>
      <fo:table-cell>
        <fo:table table-layout="fixed" padding-top="1.2cm">
          <fo:table-column column-width="3.5cm" />
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell>
                <fo:block text-align="center" padding-top="0.3cm">

                </fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </fo:table-cell>
      <fo:table-cell padding="1mm" border-width="0mm">
        <fo:table table-layout="auto" font-family="Calibri" table-width="14.5cm">
          <fo:table-column column-width="5.1cm" />
          <fo:table-column column-width="3.9cm" />
          <fo:table-column column-width="2.5cm" />
          <fo:table-column column-width="2.8cm" />
          <fo:table-header text-align="left" border-width="0mm">
            <fo:table-row height="2em" border="inherit" background-color="#D9D9D9" font-family="Arial">
              <fo:table-cell padding="1mm" padding-left="0.2cm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="left" >
                  <fo:inline>Customer Name/No</fo:inline>
                </fo:block>
              </fo:table-cell>
              <fo:table-cell padding="1mm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="left" >
                  <fo:inline>Payment Method</fo:inline>
                </fo:block>
              </fo:table-cell>
              <fo:table-cell padding="1mm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="center" >
                  <fo:inline>Despatch Date</fo:inline>
                </fo:block>
              </fo:table-cell>
              <fo:table-cell padding="1mm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="center" >
                  <fo:inline>Order No.</fo:inline>
                </fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-header>
          <fo:table-body>
            <fo:table-row border-width="4px" border-style="solid" height="2em" font-family="Segoe UI">
              <fo:table-cell padding-bottom="0.5mm" padding-left="0.2cm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="left">
                  <xsl:value-of select="CustomerDetail" />
                </fo:block>
              </fo:table-cell>
              <fo:table-cell padding-left="0.1cm" padding-bottom="0.5mm" padding-right="0.5cm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="left">
       <xsl:value-of select="Header" />
    </fo:block>
              </fo:table-cell>
              <fo:table-cell padding-left="0.8mm" padding-bottom="0.5mm" padding-right="0.5cm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="center">
                  <xsl:value-of select="Despatchdate" />
                </fo:block>
              </fo:table-cell>
              <fo:table-cell padding-left="0.1cm" padding-bottom="0.5mm" padding-right="0.5cm" border-width="1px" border-style="solid">
                <fo:block font-size="11pt" text-align="center">
                  <xsl:value-of select="OrderNumber" />
                </fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </fo:table-cell>
      <fo:table-cell padding="1mm">
        <fo:table table-layout="fixed">
          <fo:table-column column-width="4cm" />
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell>
                <fo:block text-align="center" padding-top="0.3cm">

                </fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </fo:table-cell>
    </fo:table-row>
  </fo:table-body>
</fo:table>

期望的输出

first header value 'AMEX,AMEX'

【问题讨论】:

  • 问题是什么?如何查询 XML?或者如何编写 XSLT 转换?在第二种情况下,为什么要发布 SQL 查询?
  • @PanagiotisKanavos 我想使用 xslt 获取 AMEX、AMEX 的值。我已经发布了 sql 查询来解释我如何使用东西获得 amex amex 值。
  • 这与问题无关,只会引起混乱。即使谈论 STUFF 也是错误的——东西并没有连接任何东西。您发布的是 XML 路径字符串连接查询。您可以(应该)删除对 SQL Server 和标记的 任何 引用,仍然得到相同的答案。
  • 至于i want to fetch the value of AMEX,AMEX using xslt XSLT 是一种转换 语言。它需要一个 XML 文档并生成另一个。它使用 XPath 来选择值,Michael 发布了将选择所需值的 sn-p。你想要什么作为 输出
  • 您发布的desired output 是迈克尔的回答所提供的。然而你在那里说你想要&lt;Payment&gt;AMEX, AMEX&lt;/Payment&gt;只是AMEX, AMEX

标签: xml xslt-1.0


【解决方案1】:

假设你在PackingNote的上下文中,指令:

<xsl:value-of select="Header[1]/text()"/>

将返回:

AMEX, AMEX

如果您在Header 的上下文中,请使用:

<xsl:value-of select="text()"/>

【讨论】:

  • 我已经尝试过了,但它不起作用。你能帮我在付款中添加美国运通值吗?例如 AMEX,AMEX 。那样我会更容易获得价值。
  • @Techgeeks1 这不是你在问题中要求的。
  • “将不起作用”不是对问题的良好描述。请编辑您的问题并添加minimal reproducible example
  • @Techgeeks1 您没有发布所需的输出。显然不是AMEX, AMEX。您是否期望每个标题都有不同的 &lt;Payment&gt; 元素?这将返回 two Payment 元素。还是只有第一个?
  • @Techgeeks1 您的问题仍然显示 XSLT sn-p 脱离上下文。 XSLT 非常依赖于上下文。你可以在这里看到我的答案:xsltfiddle.liberty-development.net/3NJ38Zv
猜你喜欢
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
相关资源
最近更新 更多