XSL 查询内部 xml [英] XSL query inner xml
问题描述
说这是我的 xml :
Say this is my xml :
<History>
<University>TSU</University>
<Payload>
<Attrib Order="0">OVERSEA</Attrib>
<Attrib Order="1">GRADE2</Attrib>
<Attrib Order="2"><Person><ID>TQR344</ID></Person></Attrib>
<Attrib Order="3">3566644</Attrib>
</Payload>
</History>
我想查询 Order=2 标签内的内部 XML 并读取此人的 ID.
And I want to query the inner XML inside Order=2 tag and read ID of the person.
到目前为止我已经创建了这个:
I have created this so far :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />
<xsl:template match="/History">
<xsl:apply-templates select="/History" />
</xsl:template>
<xsl:template name="Person" match="//History">
<Event>
<Uni><xsl:value-of select="University" /></Uni>
<ID><xsl:value-of select="Payload/Attrib[@Order='2']/Person/ID" disable-output-escaping="yes" /></ID>
</Event>
</xsl:template>
</xsl:stylesheet>
但是正如你所看到的,它不起作用.
But as you can see it is not working.
我还将内部 XML 分配给一个变量并尝试查询该变量,但它也不起作用.
Also I assigned the inner XML into a variable and tried to query that variable and It didn't work too.
是否可以通过 xsl 做到这一点?
Is it possible to do that via xsl ?
限制:我无法更改 xml 格式.但也许我能够从 xsl ver 1 迁移到新版本.
Limitations : I cannot change xml format. But maybe I was able to move from xsl ver 1 to new versions.
推荐答案
我想查询 Order=2 标签内的内部 XML
I want to query the inner XML inside Order=2 tag
有问题的标签不包含任何 XML;它的内容是一个字符串,需要使用字符串函数进行操作.试试:
The tag in question does not contain any XML; its content is a string and needs to be manipulated using string functions. Try:
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="/History">
<Event>
<Uni>
<xsl:value-of select="University" />
</Uni>
<ID>
<xsl:value-of select="substring-before(substring-after(Payload/Attrib[@Order='2'], '<ID>'),'</ID><')"/>
</ID>
</Event>
</xsl:template>
</xsl:stylesheet>
注意:
1.这:
<xsl:template match="/History">
<xsl:apply-templates select="/History" />
</xsl:template>
创建一个无限循环并会使您的处理器崩溃.
creates an infinite loop and will crash your processor.
2. 或者,您可以将字符串序列化回 XML 并将结果作为 XML 处理;在 XSLT 1.0 中,这只能通过输出禁用转义的字符串,将结果保存为新文档,然后使用另一个 XSLT 样式表处理新文档来完成.使用 XSLT 3.0(或支持序列化作为扩展的处理器),这一切都可以在同一个转换期间完成.
2. Alternatively, you could serialize the string back into XML and process the result as XML; in XSLT 1.0, this can be done only by outputting the string with the escaping disabled, saving the result as a new document, then processing the new document with another XSLT stylesheet. Using XSLT 3.0 (or a processor that supports serializing as an extension) this can be all done during the same transformation.
这篇关于XSL 查询内部 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!