在xslt中的属性中输出转义xml
输入xml:在xslt中的属性中输出转义xml,xml,xslt,Xml,Xslt,输入xml: <Parent> <Child attr="thing">stuff</Child> </Parent> 东西 xslt: 所需的otuput: <newChild chars="<Child attr="thing">stuff</Child>" /> 请注意,“chars”属性的值只是“Child”标记的
<Parent>
<Child attr="thing">stuff</Child>
</Parent>
东西
xslt:
所需的otuput:
<newChild chars="<Child attr="thing">stuff</Child>" />
请注意,“chars”属性的值只是“Child”标记的转义版本
问题:如何将当前匹配的元素添加到属性中?我认为。
通常会这样做,但在谈到属性时,似乎不是这样,我只是得到一些随机的xml实体,后面跟着子标记的值,例如
。我想可能需要一些逃逸的东西才能使它有效
如有任何建议,我们将不胜感激
(在每个人都问我为什么要做这样的事情之前,我受到连接到的应用程序api的限制)看起来你必须一点一点地构建它。 请注意,
。
指向父级。您可能希望创建“Child attr=”“
,追加
,”
,
,“/Child>”
,将所有属性连接起来,并使用
创建一个chars属性
比如:
<newChild >
<xsl:attribute name="chars"><Child attr="<xsl:value-of select="@attr"/>">"<value-of select="."/></Child></xsl:attribute>
</newChild>
Child attr=“”/Child
我还没查过,希望能帮上忙
然而,它非常容易出错。如果必须这样做,我可能不会使用XSLT,而是使用具有“toXML()”方法并在其上运行escapeXML()的DOM。这里是对JLRishe提到的解决方案的改编
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*" mode="asEscapedString">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text disable-output-escaping="yes"><![CDATA[="]]></xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping="yes"><![CDATA["]]></xsl:text>
</xsl:template>
<xsl:template match="*" mode="asEscapedString">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text></xsl:text>
<xsl:apply-templates select="@*" mode="asEscapedString"/>
<xsl:text>></xsl:text>
<xsl:apply-templates select="node()" mode="asEscapedString"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="Child">
<newChild>
<xsl:attribute name="chars">
<xsl:apply-templates mode="asEscapedString" select="." />
</xsl:attribute>
</newChild>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="Child"/>
</xsl:template>
</xsl:stylesheet>
/
这将生成以下输出:
<newChild chars="<Child attr=&quot;thing&quot;>stuff</Child>"/>
注意:离一般解决方案还有很远的路要走。这适用于您的简单示例 顺便说一句,没有一种简单或内置的方法可以在输出中转义XML节点树,但幸运的是,有一种方法不太复杂:
<newChild chars="<Child attr=&quot;thing&quot;>stuff</Child>"/>