XSLT转换xml
我是xsl新手。我想将xml从XSLT转换xml,xslt,Xslt,我是xsl新手。我想将xml从 <result name="response" numFound="1" start="0"> <doc> <str name="q">what</str> <arr name="suggestion"> <str>what1</str> <str>what2</str>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
什么
什么
什么
对,
什么
什么
什么
什么
我可以用
但我不知道如何将其封装到输出xml格式中。有人能帮忙吗
附加功能:
有谁能告诉我,我是否可以在输出文档中添加一个名为
的字段,该字段将随着每个文档的增加而增加100
(例如)
输出:
0
1.
在…上
“什么”
什么
什么
100
什么
什么
200
什么
什么
300
您能告诉我应该使用什么函数吗?以下样式表根据您的输入生成所需的结果。它可能过于宽松或过于严格,但因为我不知道您的确切要求,也不知道输入的模式,所以我尽了最大的努力。请将此作为起点,但进行适当的测试,并调查我提供的内容的含义
<?xml version="1.0" encoding="UTF-8"?>
<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:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="numFound"><xsl:value-of select="count(doc/arr[@name='suggestion']/str)" /></xsl:attribute>
<xsl:apply-templates select="doc/arr[@name='suggestion']" />
</xsl:copy>
</xsl:template>
<xsl:template match="arr[@name='suggestion']">
<xsl:for-each select="str">
<doc>
<xsl:copy-of select="parent::node()/preceding-sibling:: str[@name='q']" />
<xsl:variable name="nameAttributeValue" select="parent::node()/@name" />
<xsl:element name="str"><xsl:attribute name="name"><xsl:value-of select="$nameAttributeValue" /></xsl:attribute><xsl:value-of select="." /></xsl:element>
</doc>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我添加的第一个模板是确保覆盖XSLT中隐藏的一些默认模板。请注意,只有在响应
中只有一个结果
元素时,才能提供正确的输出
如前所述,请学习XSLT和XPath表达式的使用,以确保您了解样式表中的内容。以下样式表将从您的输入中生成所需的结果。它可能过于宽松或过于严格,但因为我不知道您的确切要求,也不知道输入的模式,所以我尽了最大的努力。请将此作为起点,但进行适当的测试,并调查我提供的内容的含义
<?xml version="1.0" encoding="UTF-8"?>
<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:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="numFound"><xsl:value-of select="count(doc/arr[@name='suggestion']/str)" /></xsl:attribute>
<xsl:apply-templates select="doc/arr[@name='suggestion']" />
</xsl:copy>
</xsl:template>
<xsl:template match="arr[@name='suggestion']">
<xsl:for-each select="str">
<doc>
<xsl:copy-of select="parent::node()/preceding-sibling:: str[@name='q']" />
<xsl:variable name="nameAttributeValue" select="parent::node()/@name" />
<xsl:element name="str"><xsl:attribute name="name"><xsl:value-of select="$nameAttributeValue" /></xsl:attribute><xsl:value-of select="." /></xsl:element>
</doc>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我添加的第一个模板是确保覆盖XSLT中隐藏的一些默认模板。请注意,只有在响应
中只有一个结果
元素时,才能提供正确的输出
正如我所说的,请学习XSLT和XPath表达式的使用,以确保您了解样式表中发生的事情。以下是OP评论的解决方案,它更新了问题: 此转换:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
应用于以下XML文档时(在OP的评论中提供):
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
生成所需的正确结果:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
什么
更新:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
在对其问题的更新中,OP要求:
有人能告诉我是否可以在输出文档中添加一个名为
,每增加一个值就会增加100
医生
这非常容易实现。我们只在现有转换中添加了三行代码:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
<float name="score">
<xsl:value-of select="100*position()"/>
</float>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
将此转换应用于此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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
什么
生成所需的正确结果:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
100
什么
什么
200
什么
什么
300
以下是OP评论更新问题的解决方案:
此转换:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
应用于以下XML文档时(在OP的评论中提供):
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
生成所需的正确结果:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
什么
更新:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
在对其问题的更新中,OP要求:
有人能告诉我是否可以在输出文档中添加一个名为
,每增加一个值就会增加100
医生
这非常容易实现。我们只在现有转换中添加了三行代码:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
<float name="score">
<xsl:value-of select="100*position()"/>
</float>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
将此转换应用于此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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上
“什么”
什么
什么
什么
什么
生成所需的正确结果:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="result">
<result numFound="{count(doc/arr/str)}">
<xsl:apply-templates select="@*[not(name()='numFound')]"/>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="arr[@name='suggestion']/str">
<doc>
<xsl:copy-of select="../../str"/>
<xsl:copy-of select="."/>
</doc>
</xsl:template>
<xsl:template match="doc|doc/arr">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="doc/str "/>
</xsl:stylesheet>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
</arr>
</doc>
</result>
</response>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="3" name="response" start="0">
<doc>
<str name="q">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="q">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="q">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
0
1.
在…上