簡體   English   中英

xslt:基於當前節點對后續兄弟節點進行條件更新

[英]xslt: conditional update on following sibling based on current node

我有這樣的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
 <Data>
    <D1_01>23</D1_01>
    <D1_03>2</D01_03>
    <D1_04>9</D01_04>
    <Record>
        <R9>
        <R9_01>-5</R9_01>
        <R9_02>-5</R9_02>
        <R9_05>XSLT Document Function1</R9_05>
        <R9_11>-5</R9_11>
       </R9>

        <S>
        <S_01>CC= </S_01>
        </S>
     </Record>

   <Record>
        <R9>
        <R9_01>-5</R9_01>
        <R9_02>-5</R9_02>
        <R9_05>XSLT Document Function2</R9_05>
        <R9_11>-5</R9_11>
       </R9>

        <S>
        <S_01>CC= </S_01>
        </S>
     </Record>
      .
      .
      .
    </Data>
</Root>"

對於每個記錄檢查(字符串長度(R9_05)> 10),如果為真,則將 R9_05 的文本更改為“參考”並將 R9_05 的文本附加到以下兄弟 S_01。

期望的輸出是:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
 <Data>
    <D1_01>23</D1_01>
    <D1_03>2</D01_03>
    <D1_08>8</D1_08>
    <Record>
        <R9>
        <R9_01>-5</R9_01>
        <R9_02>-5</R9_02>
        <R9_05>Reference</R9_05>
        <R9_11>-5</R9_11>
       </R9>

        <S>
        <S_01>CC=XSLT Document Function1 </S_01>
        </S>
     </Record>

   <Record>
        <R9>
        <R9_01>-5</R9_01>
        <R9_02>-5</R9_02>
        <R9_05>Reference</R9_05>
        <R9_11>-5</R9_11>
       </R9>

        <S>
        <S_01>CC=XSLT Document Function2 </S_01>
        </S>
     </Record>
      .
      .
      .
    </Data>
</Root>

下面是我的 XSLT 代碼:

<xsl:template match="Root/Data">
   <xsl:for-each select="Record">
       <xsl:variable name="var_R9_05"> 
       <xsl:choose>
           <xsl:when test="string-length(R9/R9_05) &gt; 10">
               <xsl:value-of select="concat(following-sibling::S/S_01, ' ',  R9_05)"/>
           </xsl:when>
           <xsl:otherwise>
               <xsl:value-of select="//S_01"/>
           </xsl:otherwise>
       </xsl:choose>        
       </xsl:variable>     
                <xsl:element name="R9_05"> "See narrative for complaint"  </xsl:element>  
                <xsl:element name="S_01">                         
                    <xsl:call-template name="value-to-replace">                       
                    <xsl:with-param name="param.str" select="following-sibling::S"/>  
                    <xsl:with-param name="param.target" select="following-sibling::S/S_01"/>                       
                    <xsl:with-param name="param.replacement" select="$var_R9_05)"/>                   
                    </xsl:call-template>       
                </xsl:element> 
   </xsl:for-each>
</xsl:template>

沒有得到想要的輸出,我是 XSLT 和 XML 的新手,有人可以幫我嗎?

怎么樣:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="R9_05[string-length(.) > 10]">
    <R9_05>Reference</R9_05>
</xsl:template>

<xsl:template match="S_01/text()">
    <xsl:copy/>
    <xsl:value-of select="../../../R9/R9_05[string-length(.) > 10]"/>
</xsl:template>

</xsl:stylesheet>

這可以改進,以便測試只執行一次。

暫無
暫無

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

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