XSL 1.0比较并替换另一个xml字符串中的xml元素文本

时间:2019-02-06 14:41:58

标签: xml xslt xml-parsing xslt-2.0 transformation

我有一个这样的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>

我必须通过比较TagName,用以下xml字符串中的“ Step Reference”替换整个xml中的第3个元素文本。

<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

1 个答案:

答案 0 :(得分: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>