处理<>在 XSLT 1.0 中 [英] Handling < > in XSLT 1.0

查看:23
本文介绍了处理<>在 XSLT 1.0 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试读取具有 < 的结构时遇到问题.> 在源 XML 中.

I have a problem, when trying to read a structure having < > in source XML.

输入结构 -

<?xml version="1.0" encoding="UTF-8"?>
<RecordsData>
  <RecordsData>
    <UID>&lt;RecordsData xmlns=&quot;&quot;&gt;&lt;RecordsData&gt;&lt;UID&gt;200&lt;/UID&gt;&lt;RID&gt;Test-1&lt;/RID&gt;&lt;Date&gt;20142812&lt;/Date&gt;&lt;Status&gt;N&lt;/Status&gt;&lt;/RecordsData&gt;&lt;/RecordsData&gt;</UID>
  </RecordsData>
</RecordsData>

预期输出结构(有两个要求)-一种只是 &lt; 的转换.&gt;转化为格式良好的 XML 标签.

Expected Output Structure (there are two requirements) - One is just conversion of &lt; &gt;into well formed XML tags.

<?xml version="1.0" encoding="UTF-8"?>
<RecordsData>
  <RecordsData>
    <UID><RecordsData xmlns=""><RecordsData><UID>200</UID><RID>Test-1</RID><Date>20142812</Date><Status>N</Status></RecordsData></RecordsData></UID>
  </RecordsData>
</RecordsData>

其次是提取 UID 标签内的全部数据,输出如下 -

Second is extraction of whole data inside UID tag with output as only below -

<RecordsData xmlns=""><RecordsData><UID>200</UID><RID>Test-1</RID><Date>20142812</Date><Status>N</Status></RecordsData></RecordsData>

如果我手头有第一个输出,我可以获得第二个输出.但是在对论坛进行了广泛搜索并且对 XSLT 非常陌生后,过去几天努力从 Input 获得第一个输出.

I am able to get second output if I have first one in hand. But struggling to get first output from Input over last few days after searching forum extensively and being very new to XSLT.

如果我们可以直接从输入源获得第二个输出 - 这实际上是预期的解决方案.对于上述 - 我只是试图将问题分解为步骤.

If we can directly get second output from input source - it's actually what is expected solution. For above - I just tried to break down problem into steps.

有哪位专家可以帮忙吗!

Any of experts can you please help!

谢谢,

推荐答案

转换容易,提取不易.

要将转义标记转换为真实标记,只需在将节点写入结果树时禁用转义即可,例如:

To convert the escaped markup to real markup, simply disable the escaping when writing the node to the result tree, for example:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="UID">
    <xsl:copy>
        <xsl:value-of select="." disable-output-escaping="yes"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

理想情况下,您将使用生成的 XML 文件从转义部分提取任何数据.否则,您将不得不为此应用字符串函数,因为转义文本不是 XML.

Ideally, you would use the resulting XML file to extract any data from the escaped portion. Otherwise you would have to apply string functions for this purpose, since the escaped text is not XML.

但是,在您的示例中,您不想从数据中提取任何特定内容,只需将其隔离并将其转换为独立的标记文档即可.这可以通过以下方式轻松实现:

However, in your example, you don't want to extract anything particular from the data, just isolate it and convert it to a stand-alone markup document. This can be easily accomplished by:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:value-of select="RecordsData/RecordsData/UID" disable-output-escaping="yes"/>
</xsl:template>

</xsl:stylesheet>

这篇关于处理&amp;lt;&amp;gt;在 XSLT 1.0 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆