【问题标题】:XSLT Select Node from Parameter and merge to XMLXSLT 从参数中选择节点并合并到 XML
【发布时间】:2017-06-03 03:51:27
【问题描述】:

我刚刚创建了一个 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 代码,我只是用 child id="11" 和 child id="21" 更新 frag id="10"。

我的问题是,我如何将更新后的 child id="11" 合并回 "frag id=10" 和 child id="21 to "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>

非常感谢

【问题讨论】:

    标签: xml xslt pega


    【解决方案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 输入,其中包含 section 元素的虚拟内容,正如您所写,实际内容在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      相关资源
      最近更新 更多