簡體   English   中英

XSLT:將模板與來自祖先的兄弟節點的數據一起應用

[英]XSLT: Applying a template with data from an ancestor's sibling node

我是XPathXSLT的新手,並且正在使用XSLT將XML文檔轉換為另一個XML文檔。

以下代碼顯示了源文檔的一部分:

<aggregateRoot>
   <orderRequest someAttribute="stuff">
       <!--more nodes-->
   </orderRequest>
   <order>
      <item>
        <template>
          <node>
             <image/>
          </node>
        </template>
      </item>
    </order>
<aggregateRoot>

這是我的XSLT的樣子:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <!--A bunch of stuff that works already-->   
   <Orders>
      <xsl:for-each select="aggregateRoot/order">
         <!--More Nodes-->
         <xsl:for-each select="item/template">
             <Jobs>
                 <xsl:apply-templates select="//agregateRoot/orderRequest"/>   <!--PROBLEM AREA-->
             </Jobs>
         </xsl:for-each>
      </xsl:for-each>
   </Orders>
<xsl:template/>

<xsl:template match="aggregateRoot/orderRequest">
   <!--Grab data from orderRequest and its children-->
</xsl:template>

問題描述:

在上面的XSLT,當我在里面<Jobs>節點,我想申請基於的模板<orderRequest>節點,這是一個兄弟<order>節點和主要的子<aggregateRoot>節點。

我嘗試了數十種組合來更改selectmatch語句的結構,但是我無法訪問<orderRequest>節點,甚至無法啟動第二個模板。

我看到您的XSLT有兩個問題,還有兩個較小的問題:

  • 它的格式不正確(樣本輸入也不是)
  • 您拼寫了“聚合”
  • 您正在使用雙斜杠,而您只需要一個
  • 您正在過度使用for-each而不是模板

一旦解決了前兩個問題,XSLT就可以工作。 修復了以下所有4個問題:

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

  <xsl:template match="/">
    <Orders>
      <xsl:apply-templates select="aggregateRoot/order" />
    </Orders>
  </xsl:template>

  <xsl:template match="order">
    <xsl:apply-templates select="item/template" />
  </xsl:template>

  <xsl:template match="template">
    <Jobs>
      <xsl:apply-templates select="/aggregateRoot/orderRequest"/>
    </Jobs>
  </xsl:template>

  <xsl:template match="orderRequest">
    <xsl:value-of select="@someAttribute" />
  </xsl:template>
</xsl:stylesheet>

產生輸出:

<Orders>
  <Jobs>stuff</Jobs>
</Orders>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM