Xml 将运行时创建的节点名传递给xsl:select的值
我的输入XML:Xml 将运行时创建的节点名传递给xsl:select的值,xml,xslt,xpath,Xml,Xslt,Xpath,我的输入XML: <Book> <Chapter> <Pages value="20" /> <Note1Code value="1" /> <Note1Description value="This is just for reference" /> <Note1Level value="Avg" /> <Note2Code v
<Book>
<Chapter>
<Pages value="20" />
<Note1Code value="1" />
<Note1Description value="This is just for reference" />
<Note1Level value="Avg" />
<Note2Code value="2" />
<Note2Description value="This is high important note" />
<Note2Level value="Imp" />
</Chapter>
</Book>
这是我的XSLT:
<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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[substring(name(), 1, 4) = 'Note' and substring(name(),6) = 'Code']">
<xsl:variable name="pdIdx" select="substring(name(), 5, 1)"/>
<xsl:variable name="pdNode" select="concat('Note',$pdIdx,'Description/@value')"/>
<xsl:variable name="pd" select="$pdNode"/>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="$pdNode"/>
</Description>
</xsl:template>
</xsl:stylesheet>
我对您的代码做了一些更改
<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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[contains(name(), 'Note') and contains(name(), 'Code')]">
<xsl:variable name="pdIdx" select="translate(name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '')"/>
<xsl:variable name="pdNode" select="following-sibling::*[contains(name(), concat('Note',$pdIdx,'Description'))]/@value"/>
<xsl:variable name="pdLevel" select="following-sibling::*[contains(name(), concat('Note',$pdIdx,'Level'))]/@value"/>
<xsl:variable name="pd" select="$pdNode"/>
<Note>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="$pdNode"/>
</Description>
<Level>
<xsl:value-of select="$pdLevel"/>
</Level>
</Note>
</xsl:template>
<xsl:template match="*[contains(name(), 'Description')]"/>
<xsl:template match="*[contains(name(), 'Level')]"/>
</xsl:stylesheet>
我对您的代码做了一些更改
<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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[contains(name(), 'Note') and contains(name(), 'Code')]">
<xsl:variable name="pdIdx" select="translate(name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '')"/>
<xsl:variable name="pdNode" select="following-sibling::*[contains(name(), concat('Note',$pdIdx,'Description'))]/@value"/>
<xsl:variable name="pdLevel" select="following-sibling::*[contains(name(), concat('Note',$pdIdx,'Level'))]/@value"/>
<xsl:variable name="pd" select="$pdNode"/>
<Note>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="$pdNode"/>
</Description>
<Level>
<xsl:value-of select="$pdLevel"/>
</Level>
</Note>
</xsl:template>
<xsl:template match="*[contains(name(), 'Description')]"/>
<xsl:template match="*[contains(name(), 'Level')]"/>
</xsl:stylesheet>
这看起来有点混乱,但我相信可以:
<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="*"/>
<xsl:key name="elements" match="*" use="name()"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
substring(name(),string-length(name())-3) = 'Code'
and
number(substring-before(substring-after(name(),'Note'),'Code'))]">
<xsl:variable name="Num" select="number(substring-before(substring-after(name(),'Note'),'Code'))"/>
<Note>
<Code><xsl:value-of select="key('elements', concat('Note',$Num,'Code'))/@value"/></Code>
<Description><xsl:value-of select="key('elements', concat('Note',$Num,'Description'))/@value"/></Description>
<Level><xsl:value-of select="key('elements', concat('Note',$Num,'Level'))/@value"/></Level>
</Note>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
((substring(name(),string-length(name())-10) = 'Description'
and
number(substring-before(substring-after(name(),'Note'),'Description')))
or (substring(name(),string-length(name())-4) = 'Level'
and
number(substring-before(substring-after(name(),'Note'),'Level'))))
]"/>
</xsl:stylesheet>
这看起来有点混乱,但我相信可以:
<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="*"/>
<xsl:key name="elements" match="*" use="name()"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
substring(name(),string-length(name())-3) = 'Code'
and
number(substring-before(substring-after(name(),'Note'),'Code'))]">
<xsl:variable name="Num" select="number(substring-before(substring-after(name(),'Note'),'Code'))"/>
<Note>
<Code><xsl:value-of select="key('elements', concat('Note',$Num,'Code'))/@value"/></Code>
<Description><xsl:value-of select="key('elements', concat('Note',$Num,'Description'))/@value"/></Description>
<Level><xsl:value-of select="key('elements', concat('Note',$Num,'Level'))/@value"/></Level>
</Note>
</xsl:template>
<xsl:template match="*[starts-with(name(),'Note')
and
((substring(name(),string-length(name())-10) = 'Description'
and
number(substring-before(substring-after(name(),'Note'),'Description')))
or (substring(name(),string-length(name())-4) = 'Level'
and
number(substring-before(substring-after(name(),'Note'),'Level'))))
]"/>
</xsl:stylesheet>
这难道不简单吗:
XSLT1.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="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Note') and contains(name(), 'Code')]" priority="1">
<Note>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="following-sibling::*[1]/@value"/>
</Description>
<Level>
<xsl:value-of select="following-sibling::*[2]/@value"/>
</Level>
</Note>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Note')]"/>
</xsl:stylesheet>
这是假设在源文档的其他地方没有名称以“Note”开头的元素。这难道不是简单的: XSLT1.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="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Note') and contains(name(), 'Code')]" priority="1">
<Note>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="following-sibling::*[1]/@value"/>
</Description>
<Level>
<xsl:value-of select="following-sibling::*[2]/@value"/>
</Level>
</Note>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'Note')]"/>
</xsl:stylesheet>
这是假设在源文档的其他地方没有名称以“Note”开头的元素。谢谢Rudramuni。差不多了,但在eclipse中使用时,出现以下错误:11:53:41432 error[main]JAXPSAXProcessorInvoker-表达式“funcall(matches,[funcall(name,[])、funcall(concat,[literal expr(Note)、variable ref(pdIdx/string)、literal expr(Description)]”的错误检查类型。@RudramuniTP恐怕您正在使用函数(matches())在XPath1.0但在2.0中不受支持。@Gaurav,通过将“matches”替换为“contains”进行更新,请立即检查。@Gaurav元素名称中的数字不会变为两位数(例如,
?这将使代码失败。@Rudramuni包含的
在元素为
的情况下可能不起作用,并且如果要求只处理注释和代码之间有数字的元素名。感谢Rudramuni。几乎在那里,但在eclipse中使用时,得到以下错误:11:53:41432错误[main]JAXPSAXProcessorInvoker-表达式“funcall(matches,[funcall(name,[])、funcall(concat,[literal expr(Note)、variable ref(pdIdx/string)、literal expr(Description)])]”的错误检查类型。@RudramuniTP恐怕您正在使用函数(matches())XPath1.0但2.0不支持。@Gaurav,通过将“匹配项”替换为“包含项”进行更新,请立即检查。@Gaurav元素名称中的数字不会变为两位数(例如,
?这将使代码失败。@RudramuniTP包含的
在元素为
的情况下可能不起作用,并且如果要求只处理编号介于注释和代码之间的元素名称。加上一个表示nice代码,它可以适用于所有格式的输入。感谢@Lingamurhty.-)再加上一个是漂亮的代码,它可以适用于所有格式的输入。谢谢@Lingamurhty..-)简单和完美,+1。简单和完美,+1。