[英]xsl template to get the value-of a child node at specific position in xml
First file XML1.xml 第一个文件XML1.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<akn>
<meta>
<identification source="#blah">
<FRBRWork>
<FRBRdate date="2011-11-29" name="Generation"/>
</FRBRWork>
<FRBRExpression> 123 </FRBRExpression>
</identification>
</meta>
<preface> 345</preface>
<content> 456</content>
</akn>
Second XML file 'XML2.xml' 第二个XML文件'XML2.xml'
<response>
<objects type="list">
<object>
<name>Anders</name>
<number> 98 </number>
<address>SCS</address>
</object>
<object>
<name>Anders</name>
<number> 98 </number>
<address>PQR</address>
</object>
<object>
<name>peter</name>
<number> 58 </number>
<address>ACS</address>
</object>
</objects>
</response>
My xls file is as shown below: 我的xls文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:akn="http://docs.oasis-open.org/"
exclude-result-prefixes="xs xd" version="2.0">
<!--creating shortcut to get acess cListenerOutput.xml file-->
<xsl:variable name="XML2Output"
select="document('/src/main/resources/XML2.xml')/objects"/>
<!-- For stripping the whitespaces in the output xml file -->
<xsl:strip-space elements="*"/>
<!-- For defining the output of the transformation -->
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="ISO-8859-1"/>
<xsl:template match="/">
<xsl:copy-of select="//akn:meta"/>
<header>
<docProponent>
<xsl:call-template name="cListener1"/>
</docProponent>
</header>
</xsl:template>
<xsl:template name = "cListener1">
<xsl:for-each select="$XML2Output//object">
<xsl:if test="position() = 1">
<xsl:value-of select="address"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
While, the xsl file performs the first operation correctly (extracting the node), it does not extract the value from the first instance of the object in the second file. 尽管xsl文件正确执行了第一个操作(提取节点),但它没有从第二个文件中的对象的第一个实例中提取值。
I also tried to add 我也尝试添加
<xsl:template name = "cListener1" match = "$cListenerOutput/object">
but this throws an error 但这会引发错误
Error in expression $cListenerOutput/object: Unexpected token in pattern, found $
Can someone point me to the correct approach to use the xml:variable within a template? 有人可以指出我在模板中使用xml:variable的正确方法吗?
You need to supply the full xpath in the document variable declaration: 您需要在文档变量声明中提供完整的 xpath:
<xsl:variable name="XML2Output"
select="document('/src/main/resources/XML2.xml')/response/objects"/>
With that single change, the output XML is (hopefully) as you expect: 进行了这一更改,输出XML(希望)如您所愿:
<header xmlns:akn="http://docs.oasis-open.org/">
<docProponent>SCS</docProponent>
</header>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.