xpath查询xml
我有一个输入xml,如下所示xpath查询xml,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我有一个输入xml,如下所示 <maindocument> <first> <testing>random text</testing> <checking>random test</checking> </first> <testing> <testing>sample</testing> <checking>welcome</checking> &l
<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>
随机文本
随机试验
样品
欢迎
有价值的文本
这里的输出,我想要和下面一样的输出
<maindocument>
<import>
<doctype>Valuable</doctype>
<docint>text</docint>
</import>
</maindocument
有价值的
文本
很好,将正确的元素复制到输出是很容易的,使用XSLT 2.0也很容易分解下载
元素的内容:
<xsl:output indent="yes"/>
<xsl:template match="maindocument">
<xsl:copy>
<xsl:apply-templates select="testing/import"/>
</xsl:copy>
</xsl:template>
<xsl:template match="import">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="import/downloading">
<xsl:analyze-string select="." regex="(\w)(\w+)\s+(\w+)">
<xsl:matching-substring>
<doctype>
<xsl:value-of select="concat(upper-case(regex-group(1)), regex-group(2))"/>
</doctype>
<docint>
<xsl:value-of select="regex-group(3)"/>
</docint>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
使用Saxon 9.4,上面的样式表转换输入
<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>
随机文本
随机试验
样品
欢迎
有价值的文本
进入
有价值的
文本
使用analyze string
就是一个例子,从您的示例中,我不清楚downloading
元素可以有什么类型的输入,以及您希望如何处理它。因此,如果以上内容不充分,请发布更多详细信息
With the help of absolute path and relative path my question is how i will get into testing element then i have to select import element from there.
实际上,不需要指定测试的完整路径
并检查它是否有导入
子级
XSLT处理模型()与模板和适当的模板匹配模式相结合,为您提供导航
如果没有与特定节点匹配的显式模板,则选择执行内置模板。应用内置模板的累积结果是遍历文档树,只输出文本节点
可以指定模板,以仅覆盖应以不同的特定方式处理的节点的内置模板。当内置模板为节点的子节点应用模板,并且存在与该节点匹配的显式(用户提供的)模板时,将选择此显式模板以执行/处理该节点
这个简短的XSLT 1.0转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<maindocument>
<xsl:apply-templates/>
</maindocument>
</xsl:template>
<xsl:template match="import/downloading">
<import>
<doctype><xsl:value-of select="substring-before(., ' ')"/></doctype>
<docint><xsl:value-of select="substring-after(., ' ')"/></docint>
</import>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>
<maindocument>
<import>
<doctype>valuable</doctype>
<docint>text</docint>
</import>
</maindocument>
应用于提供的XML文档时:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<maindocument>
<xsl:apply-templates/>
</maindocument>
</xsl:template>
<xsl:template match="import/downloading">
<import>
<doctype><xsl:value-of select="substring-before(., ' ')"/></doctype>
<docint><xsl:value-of select="substring-after(., ' ')"/></docint>
</import>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>
<maindocument>
<import>
<doctype>valuable</doctype>
<docint>text</docint>
</import>
</maindocument>
随机文本
随机试验
样品
欢迎
有价值的文本
生成所需的正确结果:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<maindocument>
<xsl:apply-templates/>
</maindocument>
</xsl:template>
<xsl:template match="import/downloading">
<import>
<doctype><xsl:value-of select="substring-before(., ' ')"/></doctype>
<docint><xsl:value-of select="substring-after(., ' ')"/></docint>
</import>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
<maindocument>
<first>
<testing>random text</testing>
<checking>random test</checking>
</first>
<testing>
<testing>sample</testing>
<checking>welcome</checking>
<import>
<downloading>valuable text</downloading>
</import>
</testing>
</maindocument>
<maindocument>
<import>
<doctype>valuable</doctype>
<docint>text</docint>
</import>
</maindocument>
有价值的
文本