简体   繁体   English

xslt:基于当前节点对后续兄弟节点进行条件更新

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

I have XML file like this:我有这样的 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>"

For each Record check (string-length(R9_05) > 10),if it is true then change the text of R9_05 as 'Reference' and append the text of R9_05 to the following-sibling S_01.对于每个记录检查(字符串长度(R9_05)> 10),如果为真,则将 R9_05 的文本更改为“参考”并将 R9_05 的文本附加到以下兄弟 S_01。

Desired output is:期望的输出是:

<?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>

Below is my XSLT code:下面是我的 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>

Not getting desired output, I am new to XSLT and XML, Can someone please help me?没有得到想要的输出,我是 XSLT 和 XML 的新手,有人可以帮我吗?

How about:怎么样:

XSLT 1.0 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>

This could be improved so that the test is performed only once.这可以改进,以便测试只执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM