XSLT:如何使用XSLT 1.0和XALAN处理器转换部分转义的XML?

4

我有这样的代码:

<root>
<row>
    <field>&amp;lt;![CDATA[&amp;lt;comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema"&amp;gt;
        &amp;lt;inicioCFD&amp;gt;
        &amp;lt;idArchivo&amp;gt;182NAI053402&amp;lt;/idArchivo&amp;gt;
        &amp;lt;etiquetaCFD&amp;gt;NCR&amp;lt;/etiquetaCFD&amp;gt;
        &amp;lt;/inicioCFD&amp;gt;
        &amp;lt;/comprobante&amp;gt;]]&amp;gt;</field>
</row>
</root>

我需要这个:

<comprobante>
  <idArchivo etiquetaCFD="NCR">182NAI053402</idArchivo>
</comprobante>

我正在使用以下这个xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:a="http://www.tralix.com/cfd/2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xmlns:xalan="http://xml.apache.org/xalan"
extension-element-prefixes="exsl xalan">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="/root/row/field">
    <xsl:variable name="comprobante_">
        <xsl:variable name="p6">
            <xsl:variable name="p5">
                <xsl:variable name="p4">
                    <xsl:variable name="p3">
                        <xsl:variable name="p2">
                            <xsl:variable name="p1">
                                <xsl:value-of select="substring-before(substring-after(.,'CDATA['),']]')"/>
                            </xsl:variable>
                            <xsl:call-template name="replace-string">
                                <xsl:with-param name="text" select="$p1"/>
                                <xsl:with-param name="replace" select="'gt;'" />
                                <xsl:with-param name="with" select="'¬'"/>
                            </xsl:call-template>
                        </xsl:variable>
                        <xsl:call-template name="replace-string">
                            <xsl:with-param name="text" select="$p2"/>
                            <xsl:with-param name="replace" select="'lt;'"/>
                            <xsl:with-param name="with" select="'~'"/>
                        </xsl:call-template>
                    </xsl:variable>
                    <xsl:call-template name="replace-string">
                        <xsl:with-param name="text" select="$p3"/>
                        <xsl:with-param name="replace" select="'&amp;~'"/>
                        <xsl:with-param name="with" select="'&lt;'"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:call-template name="replace-string">
                    <xsl:with-param name="text" select="$p4"/>
                    <xsl:with-param name="replace" select="'&amp;¬'"/>
                    <xsl:with-param name="with" select="'&gt;'"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:value-of select="$p5" disable-output-escaping="yes"/>
        </xsl:variable>
        <xsl:copy-of select="xalan:nodeset($p6)"/>
    </xsl:variable>
    <xsl:variable name="comprobante" select="xalan:nodeset($comprobante_)"/>
    <comprobante>
      <idArchivo>
          <xsl:attribute name="etiquetaCFD">
              <xsl:value-of select="$comprobante/comprobante/inicioCFD/etiquetaCFD"/>
          </xsl:attribute>
              <xsl:value-of select="$comprobante/comprobante/inicioCFD/idArchivo"/>
      </idArchivo>  
    </comprobante>
       </xsl:template>
<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
        <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$with"/>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text"
                    select="substring-after($text,$replace)"/>
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="with" select="$with"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

它会生成这样的结果:

<comprobante>
  <idArchivo etiquetaCFD=""></idArchivo>
</comprobante>

空值是因为转义的XML不像post XSLT: How to transform partially escaped XML? 中所说的那样,所以我无法从我的$comprobante变量中读取任何内容。

但在那篇文章中,Dimitri说可以使用saxon:parse()。嗯,我正在使用Xalan处理器,但我找不到类似的东西。我限于使用xalan和xslt 1.0。

需要帮助吗?

提前谢谢!

2个回答

1

像这样,会从field标签内提取已转义的内容,并将其作为纯文本输出(该文本恰好是格式良好的XML):

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

  <xsl:template match="/">
    <xsl:variable name="withoutCDataStart"
       select="substring(root/row/field, 13)" />
    <xsl:variable name="withoutCDataEnd"
       select="substring($withoutCDataStart, 1,
                         string-length($withoutCDataStart) - 6)" />

    <xsl:call-template name="unescape">
      <xsl:with-param name="text" select="$withoutCDataEnd" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="unescape">
    <xsl:param name="text" />
    <xsl:choose>
      <xsl:when test="contains($text, '&amp;')">
        <xsl:value-of select="substring-before($text, '&amp;')" />
        <xsl:variable name="afterAmp" select="substring-after($text, '&amp;')" />
        <xsl:choose>
          <xsl:when test="starts-with($afterAmp, 'amp;')">&amp;</xsl:when>
          <xsl:when test="starts-with($afterAmp, 'lt;')">&lt;</xsl:when>
          <xsl:when test="starts-with($afterAmp, 'gt;')">&gt;</xsl:when>
          <xsl:when test="starts-with($afterAmp, 'quot;')">"</xsl:when>
          <xsl:when test="starts-with($afterAmp, 'apos;')">'</xsl:when>
        </xsl:choose>
        <xsl:call-template name="unescape">
          <xsl:with-param name="text" select="substring-after($afterAmp, ';')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

你需要将这个输出反馈到另一个样式表中,以执行你所需的实际转换。

非常感谢,但我需要使用一个样式表来获取输出。这可能吗? - jechaviz
仅通过调用Java扩展函数(类似于saxon:parse())来将词法XML解析为树形结构。为什么您对解决方案施加了这么多限制?为什么您仍在使用XSLT 1.0? - Michael Kay
我正在集成一个系统(不是由我编写的),它仅使用Java默认引擎XSLT,即Xalan。它可以接受任何XSLT,但需要用XSLT 1.0编写。我想更改该系统,但我无法这样做,因此我需要使用XSLT 1.0实现两个系统。 - jechaviz
我的问题是关于是否存在类似于Xalan中的saxon:parse()函数;我在Xalan API(http://xml.apache.org/xalan-j/apidocs/index.html)和Xalan Library(http://xml.apache.org/xalan-j/extensionslib.html)中浏览过,但我感到相当迷茫。 - jechaviz
看到这篇文章https://dev59.com/jm3Xa4cB1Zd3GeqPg6Et,我发现无法使用XSLT 1.0或XSLT 2.0来完成,所以唯一的希望是使用xalan的扩展。 - jechaviz
@MichaelKay 我看到你在这里的另一个答案:http://stackoverflow.com/questions/15276930/how-to-traverse-after-disable-output-escaping。因为我的xml是可预测的,所以我可以使用字符串操作!我之前想不出来!谢谢你的帮助,我会在这里发布我的工作。 - jechaviz

0

在@IanRoberts和@MichaelKay的帮助下,我弄清楚了如何创建一个转义的XML解析器,这个工作的XSLT就是结果。感谢你们的帮助!

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common" 
extension-element-prefixes="exsl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template match="/">
    <xsl:variable name="withoutCDataStart"
        select="substring-after(root/row/field, '&amp;lt;![CDATA[')"/>
    <xsl:variable name="withoutCDataEnd"
        select="substring-before($withoutCDataStart, ']]&amp;gt;')"/>
    <xsl:variable name="unEscapedXml">
        <xsl:call-template name="unescape">
            <xsl:with-param name="text" select="$withoutCDataEnd"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="parsedXml_">
        <xsl:call-template name="parseXml">
            <xsl:with-param name="text" select="$unEscapedXml"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="parsedXml" select="exsl:node-set($parsedXml_)"/>
    <comprobante>
        <idArchivo>
            <xsl:attribute name="etiquetaCFD">
                <xsl:value-of select="$parsedXml/comprobante/inicioCFD/etiquetaCFD"/>
            </xsl:attribute>
            <xsl:value-of select="$parsedXml/comprobante/inicioCFD/idArchivo"/>
        </idArchivo>  
    </comprobante>
</xsl:template>
<xsl:template name="unescape">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&amp;')">
            <xsl:value-of select="substring-before($text, '&amp;')"/>
            <xsl:variable name="afterAmp" select="substring-after($text, '&amp;')"/>
            <xsl:choose>
                <xsl:when test="starts-with($afterAmp, 'amp;')">&amp;</xsl:when>
                <xsl:when test="starts-with($afterAmp, 'lt;')">&lt;</xsl:when>
                <xsl:when test="starts-with($afterAmp, 'gt;')">&gt;</xsl:when>
                <xsl:when test="starts-with($afterAmp, 'quot;')">"</xsl:when>
                <xsl:when test="starts-with($afterAmp, 'apos;')">'</xsl:when>
            </xsl:choose>
            <xsl:call-template name="unescape">
                <xsl:with-param name="text" select="substring-after($afterAmp, ';')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="parseXml">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&gt;')">
            <xsl:variable name="topLevelTag">
                <xsl:call-template name="getTopLevelTag">
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="openingTag">
                <xsl:value-of select="$topLevelTag"/>
            </xsl:variable>
            <xsl:variable name="tagName">
                <xsl:call-template name="getTopLevelTagName">
                    <xsl:with-param name="text" select="$text"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="closingTag">
                <xsl:value-of select="concat('&lt;/',$tagName,'&gt;')"/>
            </xsl:variable>
            <xsl:variable name="firstNode">
                <xsl:if test="not(contains($topLevelTag,'/&gt;'))">
                    <xsl:value-of select="substring-before(substring-after($text,$openingTag),$closingTag)"/>        
                </xsl:if>
            </xsl:variable>
            <xsl:variable name="afterFirstNode">
                <xsl:choose>
                    <xsl:when test="not(contains($topLevelTag,'/&gt;'))">
                        <xsl:value-of select="substring-after($text,concat($firstNode,$closingTag))"/>        
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="substring-after($text,$topLevelTag)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:element name="{$tagName}">
                <xsl:call-template name="createAttributes">
                    <xsl:with-param name="text" select="$topLevelTag"/>
                </xsl:call-template>
                <xsl:call-template name="parseXml">
                    <xsl:with-param name="text" select="$firstNode"/>
                </xsl:call-template>
            </xsl:element>
            <xsl:call-template name="parseXml">
                <xsl:with-param name="text" select="$afterFirstNode"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<xsl:template name="getTopLevelTagName">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&gt;')">
            <xsl:variable name="tagWithAttributesWithoutEnd">
                <xsl:value-of select="substring-before($text, '&gt;')"/>
            </xsl:variable>
            <xsl:variable name="tagWithAttributesWithoutBegining">
                <xsl:value-of select="substring-after($tagWithAttributesWithoutEnd, '&lt;')"/>
            </xsl:variable>
            <xsl:variable name="tagName">
                <xsl:choose>
                    <xsl:when test="contains($tagWithAttributesWithoutBegining,' ')">
                        <xsl:value-of
                            select="substring-before($tagWithAttributesWithoutBegining, ' ')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$tagWithAttributesWithoutBegining"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:value-of select="$tagName"/>
        </xsl:when>
    </xsl:choose>
</xsl:template>
<xsl:template name="getTopLevelTag">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&gt;')">
            <xsl:variable name="tagWithAttributesWithoutEnd">
                <xsl:value-of select="substring-before($text, '&gt;')"/>
            </xsl:variable>
            <xsl:value-of select="concat($tagWithAttributesWithoutEnd,'&gt;')"/>
        </xsl:when>
    </xsl:choose>
</xsl:template>
<xsl:template name="createAttributes">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '=&quot;')">
            <xsl:variable name="attributeName">
                <xsl:value-of select="substring-before(substring-after($text,' '),'=&quot;')"/>
            </xsl:variable>
            <xsl:message>
                <xsl:value-of select="$text"/>
            </xsl:message>
            <xsl:variable name="attributeValue">
                <xsl:value-of select="substring-before(substring-after($text,concat($attributeName,'=&quot;')),'&quot;')"/>
            </xsl:variable>
            <xsl:attribute name="{$attributeName}">
                <xsl:value-of select="$attributeValue"/>
            </xsl:attribute>
            <xsl:call-template name="createAttributes">
                <xsl:with-param name="text" select="substring-after($text,concat($attributeName,'=&quot;',$attributeValue,'&quot;'))"/>
            </xsl:call-template>
        </xsl:when>
    </xsl:choose>        
</xsl:template>
</xsl:stylesheet>

它产生了我所需的输出:
<comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <idArchivo etiquetaCFD="NCR">182NAI053402</idArchivo>
</comprobante>

我发布我的作品,希望能对其他人有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接