禁用输出转义后如何遍历 [英] How to traverse after disable-output-escaping
问题描述
我有一个这样的 XML -
I have a XML like this -
<DOCUMENT>
<SERVICE>
<ID>1338</ID>
<NAME>
<EN>this is an english name</EN>
<DE>this is a german name</DE>
</NAME>
</SERVICE>
</DOCUMENT>
正如您所看到的,名称标签内的元素类似于 XML,但并未真正格式化为元素.输出 XML 需要看起来像
As you can see the elements inside the name tag are XML like but not really formatted as elements. The output XML needs to look like
<SERVICES>
<SERVICE ID="1338" EN="this is an english name" DE="this is a german name"/>
</SERVICES>
我正在尝试通过 XPATH 获取 EN 和 DE 的值.我曾尝试使用禁用输出转义,但我认为这行不通.
I am trying to get the value of the EN and DE through XPATH. I have tried to playaround with disable-output-escaping but I dont think that will work.
<xsl:template match="/">
<SERVICES>
<SERVICE>
<xsl:attribute name="ID"><xsl:value-of select="DOCUMENT/SERVICE/ID"/></xsl:attribute>
<xsl:attribute name="EN"><xsl:value-of select="DOCUMENT/SERVICE/NAME/EN" disable-output-escaping="yes"/></xsl:attribute>
<xsl:attribute name="DE"><xsl:value-of select="DOCUMENT/SERVICE/NAME/DE" disable-output-escaping="yes"/></xsl:attribute>
</SERVICE>
</SERVICES>
</xsl:template>
这里有什么建议吗?
推荐答案
首选方法是将 NAME 元素的字符串内容通过 XML 解析器将其转换为节点树.如果您的处理器支持诸如 saxon:parse()(或 XPath 3.0 parse-xml())之类的扩展,或者调用扩展函数,则可以完成此操作.
The preferred approach is to take the string content of the NAME element and put it through an XML parser to turn it into a node tree. This can be done if your processor supports an extension such as saxon:parse() (or XPath 3.0 parse-xml()), or by calling out to an extension function.
如果内部 XML 非常固定和可预测,那么您或许可以通过直接字符串操作来解析它.
If the internal XML is very stereotyped and predictable, then you could perhaps parse it by direct string manipulation.
这篇关于禁用输出转义后如何遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!