XSL 1.0比较,并从另一个XML字符串替换XML元素文本

问题描述 投票:0回答:1

我有这样的XML表:

<tr>
    <td><font face="Calibri">Tank CIP warning interval (Hours)</font></td>
    <td><font face="Calibri">||MIN_CIP_EXPIRE_TIME||</font></td>
    <td><font face="Calibri">CIP Check - Tank Filter</font></td>
    <td><font face="Calibri">Equipment</font></td>
    <td><font face="Calibri">True</font></td>
</tr>
<tr>
    <td><font face="Calibri">Vent Filter Material ID</font></td>
    <td><font face="Calibri">||MM_EXPECTED_FILTER_MATL_ID_01||</font></td>
    <td><font face="Calibri">Scan/Install/Consume Vent Filters</font></td>
    <td><font face="Calibri">Materials</font></td>
    <td><font face="Calibri">False</font></td>
</tr>

我通过比较标签名,以取代从以下XML字符串“步骤参考”在整个XML第三元素的文本。

<Data>
    <row>
        <BoPName>OP_R_CFG_DEFINED_WEIGH_SEQUENCE</BoPName>
        <TagName>||MIN_CIP_EXPIRE_TIME||</TagName>
        <StepReference>Calculate Expiry</StepReference>
    </row>
    <row>
        <BoPName>OP_R_CFG_FIRST_ADD_EXP_INTERVAL</BoPName>
        <TagName>||MM_EXPECTED_FILTER_MATL_ID_01||</TagName>
        <StepReference>Scan Material</StepReference>
   </row>
<Data>

该CIP检查 - 谐振滤波器必须与计算到期更换。

Please refer this picture

xml xslt xml-parsing xslt-2.0 transformation
1个回答
1
投票

假设你想XSLT 1.0没有2.0,事遂所愿

<table>
 <tr>
    <td><font face="Calibri">Tank CIP warning interval (Hours)</font></td>
    <td><font face="Calibri">||MIN_CIP_EXPIRE_TIME||</font></td>
    <td><font face="Calibri">CIP Check - Tank Filter</font></td>
    <td><font face="Calibri">Equipment</font></td>
    <td><font face="Calibri">True</font></td>
</tr>
<tr>
    <td><font face="Calibri">Vent Filter Material ID</font></td>
    <td><font face="Calibri">||MM_EXPECTED_FILTER_MATL_ID_01||</font></td>
    <td><font face="Calibri">Scan/Install/Consume Vent Filters</font></td>
    <td><font face="Calibri">Materials</font></td>
    <td><font face="Calibri">False</font></td>
</tr>
</table>

与数据文件tab2.xml

<Data>
    <row>
        <BoPName>OP_R_CFG_DEFINED_WEIGH_SEQUENCE</BoPName>
        <TagName>||MIN_CIP_EXPIRE_TIME||</TagName>
        <StepReference>Calculate Expiry</StepReference>
    </row>
    <row>
        <BoPName>OP_R_CFG_FIRST_ADD_EXP_INTERVAL</BoPName>
        <TagName>||MM_EXPECTED_FILTER_MATL_ID_01||</TagName>
        <StepReference>Scan Material</StepReference>
   </row>
</Data>

您可以构建一个键查找等

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:variable name="data" select="document('tab2.xml')"/>

 <xsl:key name="k" match="StepReference" use="../TagName"/>

 <xsl:template match="node()">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>


 <xsl:template match="td/font/text()">
  <xsl:variable name="here" select="."/>
  <xsl:variable name="tn" select="../../preceding-sibling::*[1]/font"/>
  <xsl:for-each select="$data">
   <xsl:choose>
    <xsl:when test="key('k',$tn)">
     <xsl:value-of select="key('k',$tn)"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$here"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each>
 </xsl:template>


</xsl:stylesheet>

生产:

$ xsltproc tab.xsl tab.xml
<?xml version="1.0"?>
<table>
 <tr>
    <td><font face="Calibri">Tank CIP warning interval (Hours)</font></td>
    <td><font face="Calibri">||MIN_CIP_EXPIRE_TIME||</font></td>
    <td><font face="Calibri">Calculate Expiry</font></td>
    <td><font face="Calibri">Equipment</font></td>
    <td><font face="Calibri">True</font></td>
</tr>
<tr>
    <td><font face="Calibri">Vent Filter Material ID</font></td>
    <td><font face="Calibri">||MM_EXPECTED_FILTER_MATL_ID_01||</font></td>
    <td><font face="Calibri">Scan Material</font></td>
    <td><font face="Calibri">Materials</font></td>
    <td><font face="Calibri">False</font></td>
</tr>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.