1

我刚刚创建了一个 XSL 转换,我将 Xml 转换为 xHTML 并返回。这是我第一次使用 XSLT。现在我正在构建“后部”一个反向转换,我被卡住了。

简介/概述

因此,基本 XML 包含我要查找的两个节点(片段):

<frag id="10" name="Editable_Fragment" >
 <child id="11"></child>
</frag>
<frag id="20" name="Editable_Fragment2">
 <child id="21"></child>
</frag>

顺便说一句,这个 XML 中有很多片段,但我只是在寻找“可编辑”的片段!所以我创建了一个这样的 XSLT:

<?xml version="1.0"?>

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

<xsl:template match="/">

  <xsl:apply-templates select="/review-case/review-document/review-channel/content/region/section/frag[@name='Editable_Fragment']/node()"/>
  <xsl:apply-templates select="/review-case/review-document/review-channel/content/region/section/frag[@name='Editable_Fragment2']/node()"/>

</xsl:template>

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

两个节点的内容在富文本编辑器中作为字符串保存在一起!内容如下所示:

<child id="11" name="Editable_Fragment">....data...</child>
<child id="21" name="Editable_Fragment">....data...</child>

在富文本编辑器中,我正在更改两个节点的一些数据,然后我想使用所谓的反向转换来更新数据。

逆向转换问题

带有两个“子”标签的字符串将用于参数mpTransformParameters中的进一步处理。我必须使用这个参数。我知道通过以下 XSLT 代码,我只是用子 id="11" 和子 id="21" 更新 frag id="10"。

我的问题是,如果两者都在字符串中,我如何将更新的 child id="11" 合并回 "frag id=10" 和 child id="21 到 "frag id=20" ?

“反向”XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output indent="yes"/>
 <xsl:param name="mpTransformParameters"/>

<xsl:template match="review-case/review-document/review-channel/content/region/section/frag/child[@name='Editable_Fragment']">
 <xsl:value-of select="$mpTransformParameters" disable-output-escaping="yes"/>          
</xsl:template>

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

</xsl:stylesheet>

非常感谢

4

1 回答 1

1

试试这样的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" omit-xml-declaration="yes"/>
  <xsl:param name="mpTransformParameters">
    &lt;child id="11" name="Editable_Fragment"&gt;..data 11..&lt;/child&gt;
    &lt;child id="21" name="Editable_Fragment"&gt;..data 22..&lt;/child&gt;
  </xsl:param>

  <xsl:template match="section">
    <xsl:variable name="tbl"
      select="tokenize($mpTransformParameters, '&lt;/child&gt;')"/>
    <xsl:copy>
      <xsl:for-each select="$tbl">
        <xsl:analyze-string select="."
          regex="id=&quot;(\d+)&quot;.*&gt;(.*)">
          <xsl:matching-substring>
            <xsl:variable name="id" select="number(regex-group(1)) - 1"/>
            <xsl:variable name="txt" select="regex-group(2)"/>
            <xsl:variable name="nm">
              <xsl:if test="$id=10"><xsl:text>Editable_Fragment</xsl:text></xsl:if>
              <xsl:if test="$id=20"><xsl:text>Editable_Fragment2</xsl:text></xsl:if>
            </xsl:variable>
            <xsl:element name="frag">
              <xsl:attribute name="id" select="$id"/>
              <xsl:attribute name="name" select="$nm"/>
              <xsl:value-of select="$txt"/>
            </xsl:element>
          </xsl:matching-substring>
        </xsl:analyze-string>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

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

我特意选择了XSLT 2.0 ,因为在1.0版本中写起来太乏味了。

出于测试运行的目的,我为mpTransformParameter分配了一个示例值。

正如您所写,我准备了带有部分元素的虚拟内容的稍微简化的 XML 输入,实际内容在mpTransformParameters中:

<region>
  <section>Dummy content
  </section>
</region>

使用它,我得到以下结果:

<region>
  <section>
      <frag id="10" name="Editable_Fragment">..data 11..</frag>
      <frag id="20" name="Editable_Fragment2">..data 22..</frag>
   </section>
</region>
于 2017-01-20T00:04:38.527 回答