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

XSLT:如何使用XSLT1.0和XALAN处理器转换部分转义的XML?,xml,xslt,xalan,Xml,Xslt,Xalan,我有这个: <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&a

我有这个:

<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>

&;书信电报;![CDATA[<;comprobante xmlns:xsi=”http://www.w3.org/2001/XMLSchema“&;gt;
&<;inicioCFD>;
&<;爱达荷>;182NAI053402<;爱达荷>;
&<;etiquetaCFD>;NCR<;/etiquetaCFD>;
&<;/inicioCFD>;
&;lt;/comprobante&;gt;]];燃气轮机;
我需要这个:

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

182NAI053402
我正在使用这个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所说的XML,所以我无法从$compobante变量中读取任何内容

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

有什么帮助吗


提前感谢

类似的内容将从
字段中提取转义内容
并将其作为纯文本输出(这恰好是格式良好的XML):


&;
"
'

然后,您必须将此输出反馈到另一个样式表中,以执行您想要的实际转换。

@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>

182NAI053402

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

非常感谢,但我需要用一个样式表获得输出。这是可能的吗?只需要调用Java扩展函数(按照saxon:parse()的思路),将词法XML解析为树。为什么要对解决方案施加这么多限制?为什么您仍然使用XSLT1.0?我正在集成一个仅使用java默认引擎XSLT的系统(不是我自己编程的),这就是Xalan。它接受任何xslt,但需要用XSLT1.0编写。我想更改系统,但我不能,所以我需要使用XSLT1.0实现两个系统;我浏览了XalanAPI()和XalanLibrary(),但我很迷茫。看到这篇文章,我发现XSLT1.0或XSLT2.0是做不到的,所以唯一的希望是使用Xalan的扩展
<comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <idArchivo etiquetaCFD="NCR">182NAI053402</idArchivo>
</comprobante>