我有这个:
<root>
<row>
<field>&lt;![CDATA[&lt;comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema"&gt;
&lt;inicioCFD&gt;
&lt;idArchivo&gt;182NAI053402&lt;/idArchivo&gt;
&lt;etiquetaCFD&gt;NCR&lt;/etiquetaCFD&gt;
&lt;/inicioCFD&gt;
&lt;/comprobante&gt;]]&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="'&~'"/>
<xsl:with-param name="with" select="'<'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$p4"/>
<xsl:with-param name="replace" select="'&¬'"/>
<xsl:with-param name="with" select="'>'"/>
</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不是像帖子XSLT: How to transform partially escaped XML?那样的XML,所以我无法从我的$ comprobante变量中读取任何内容。
但是在那篇文章中,Dimitri说它可以用saxon:parse()。好吧,我正在使用Xalan处理器,但我找不到类似的东西。我只能使用xalan和xslt 1.0。
任何帮助?
提前致谢
答案 0 :(得分: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, '&')">
<xsl:value-of select="substring-before($text, '&')" />
<xsl:variable name="afterAmp" select="substring-after($text, '&')" />
<xsl:choose>
<xsl:when test="starts-with($afterAmp, 'amp;')">&</xsl:when>
<xsl:when test="starts-with($afterAmp, 'lt;')"><</xsl:when>
<xsl:when test="starts-with($afterAmp, '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>
然后,您必须将此输出反馈到另一个样式表,以进行所需的实际转换。
答案 1 :(得分:0)
<?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, '&lt;![CDATA[')"/>
<xsl:variable name="withoutCDataEnd"
select="substring-before($withoutCDataStart, ']]&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, '&')">
<xsl:value-of select="substring-before($text, '&')"/>
<xsl:variable name="afterAmp" select="substring-after($text, '&')"/>
<xsl:choose>
<xsl:when test="starts-with($afterAmp, 'amp;')">&</xsl:when>
<xsl:when test="starts-with($afterAmp, 'lt;')"><</xsl:when>
<xsl:when test="starts-with($afterAmp, '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, '>')">
<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('</',$tagName,'>')"/>
</xsl:variable>
<xsl:variable name="firstNode">
<xsl:if test="not(contains($topLevelTag,'/>'))">
<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,'/>'))">
<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, '>')">
<xsl:variable name="tagWithAttributesWithoutEnd">
<xsl:value-of select="substring-before($text, '>')"/>
</xsl:variable>
<xsl:variable name="tagWithAttributesWithoutBegining">
<xsl:value-of select="substring-after($tagWithAttributesWithoutEnd, '<')"/>
</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, '>')">
<xsl:variable name="tagWithAttributesWithoutEnd">
<xsl:value-of select="substring-before($text, '>')"/>
</xsl:variable>
<xsl:value-of select="concat($tagWithAttributesWithoutEnd,'>')"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="createAttributes">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '="')">
<xsl:variable name="attributeName">
<xsl:value-of select="substring-before(substring-after($text,' '),'="')"/>
</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,'="')),'"')"/>
</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,'="',$attributeValue,'"'))"/>
</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>
我发表我的作品,希望对任何其他人都有帮助。