Xml 如何在xslt中应用引用值的模板?
下面是我刚刚编造的一个例子(所以不要以打字错误或缩进来评判我):-Xml 如何在xslt中应用引用值的模板?,xml,xslt,xpath,xls,Xml,Xslt,Xpath,Xls,下面是我刚刚编造的一个例子(所以不要以打字错误或缩进来评判我):- 我对你的问题提出以下答复: 输入: :::::::::::::: companies.xml :::::::::::::: <?xml version="1.0" encoding="UTF-8"?> <company id="c123" name="comp 1"> <buildings> <building id="b1" name="bld1"> <depa
我对你的问题提出以下答复: 输入:
::::::::::::::
companies.xml
::::::::::::::
<?xml version="1.0" encoding="UTF-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department ref="d1"/>
<department ref="d3"/>
</building>
</buildings>
<departments>
<department id="d1" name="IT">
<desc>Lorem ipsum</desc>
<cto>Mr. X</cto>
</department>
<department id="d2" name="HR">
<desc>Lorem ipsum</desc>
<coo>Mr. Y</coo>
</department>
<department id="d3" name="Finance">
<desc>Lorem ipsum</desc>
<cfo>Mr. Z</cfo>
</department>
</departments>
</company>
::::::::::::::
companies.xsl
::::::::::::::
<?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" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When matching department: do nothing -->
<xsl:template match="departments" />
<xsl:template match="/departments/department" />
<xsl:template match="building">
<!-- create the node building with attributes -->
<building id="{./@id}" name="{./@name}">
<!-- loop on each department and extract the relevant data -->
<xsl:for-each select="./department">
<xsl:variable name="refvar" select="./@ref"/>
<department id="{./@ref}" name="{//department[@id=$refvar]/@name}" desc="{//department[@id=$refvar]/desc}">
<xsl:attribute name="{name(//department[@id=$refvar]/*[position() = 2])}">
<xsl:value-of select="//department[@id=$refvar]/*[position() = 2]"/>
</xsl:attribute>
</department>
</xsl:for-each>
</building>
</xsl:template>
</xsl:stylesheet>
$ xsltproc companies.xsl companies.xml | xmllint --format -
<?xml version="1.0" encoding="utf-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department id="d1" name="IT" desc="Lorem ipsum" cto="Mr. X"/>
<department id="d3" name="Finance" desc="Lorem ipsum" cfo="Mr. Z"/>
</building>
</buildings>
</company>
:
companies.xml
::::::::::::::
乱数假文
X先生
乱数假文
Y先生
乱数假文
Z先生
样式表文件:
::::::::::::::
companies.xml
::::::::::::::
<?xml version="1.0" encoding="UTF-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department ref="d1"/>
<department ref="d3"/>
</building>
</buildings>
<departments>
<department id="d1" name="IT">
<desc>Lorem ipsum</desc>
<cto>Mr. X</cto>
</department>
<department id="d2" name="HR">
<desc>Lorem ipsum</desc>
<coo>Mr. Y</coo>
</department>
<department id="d3" name="Finance">
<desc>Lorem ipsum</desc>
<cfo>Mr. Z</cfo>
</department>
</departments>
</company>
::::::::::::::
companies.xsl
::::::::::::::
<?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" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When matching department: do nothing -->
<xsl:template match="departments" />
<xsl:template match="/departments/department" />
<xsl:template match="building">
<!-- create the node building with attributes -->
<building id="{./@id}" name="{./@name}">
<!-- loop on each department and extract the relevant data -->
<xsl:for-each select="./department">
<xsl:variable name="refvar" select="./@ref"/>
<department id="{./@ref}" name="{//department[@id=$refvar]/@name}" desc="{//department[@id=$refvar]/desc}">
<xsl:attribute name="{name(//department[@id=$refvar]/*[position() = 2])}">
<xsl:value-of select="//department[@id=$refvar]/*[position() = 2]"/>
</xsl:attribute>
</department>
</xsl:for-each>
</building>
</xsl:template>
</xsl:stylesheet>
$ xsltproc companies.xsl companies.xml | xmllint --format -
<?xml version="1.0" encoding="utf-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department id="d1" name="IT" desc="Lorem ipsum" cto="Mr. X"/>
<department id="d3" name="Finance" desc="Lorem ipsum" cfo="Mr. Z"/>
</building>
</buildings>
</company>
:
companies.xsl
::::::::::::::
输出:
::::::::::::::
companies.xml
::::::::::::::
<?xml version="1.0" encoding="UTF-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department ref="d1"/>
<department ref="d3"/>
</building>
</buildings>
<departments>
<department id="d1" name="IT">
<desc>Lorem ipsum</desc>
<cto>Mr. X</cto>
</department>
<department id="d2" name="HR">
<desc>Lorem ipsum</desc>
<coo>Mr. Y</coo>
</department>
<department id="d3" name="Finance">
<desc>Lorem ipsum</desc>
<cfo>Mr. Z</cfo>
</department>
</departments>
</company>
::::::::::::::
companies.xsl
::::::::::::::
<?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" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When matching department: do nothing -->
<xsl:template match="departments" />
<xsl:template match="/departments/department" />
<xsl:template match="building">
<!-- create the node building with attributes -->
<building id="{./@id}" name="{./@name}">
<!-- loop on each department and extract the relevant data -->
<xsl:for-each select="./department">
<xsl:variable name="refvar" select="./@ref"/>
<department id="{./@ref}" name="{//department[@id=$refvar]/@name}" desc="{//department[@id=$refvar]/desc}">
<xsl:attribute name="{name(//department[@id=$refvar]/*[position() = 2])}">
<xsl:value-of select="//department[@id=$refvar]/*[position() = 2]"/>
</xsl:attribute>
</department>
</xsl:for-each>
</building>
</xsl:template>
</xsl:stylesheet>
$ xsltproc companies.xsl companies.xml | xmllint --format -
<?xml version="1.0" encoding="utf-8"?>
<company id="c123" name="comp 1">
<buildings>
<building id="b1" name="bld1">
<department id="d1" name="IT" desc="Lorem ipsum" cto="Mr. X"/>
<department id="d3" name="Finance" desc="Lorem ipsum" cfo="Mr. Z"/>
</building>
</buildings>
</company>
$xsltproc companys.xsl companys.xml | xmllint--格式-
您能否添加预期输出并更正xml文件,因为它的格式不正确…@Allan添加了一个示例预期输出。不太关心输出,但如何从内部联系部门向新来者建议:如果答案解决了您的问题,请单击大复选标记接受答案(✓) 在它旁边,也可以选择向上投票(向上投票要求至少15个信誉点)。如果您发现其他答案有帮助,请向上投票。接受和向上投票有助于未来的读者。请参阅[相关帮助中心文章][1][1]:事实上,我会根据打字错误和缩进来判断你。如果你不在意这些细节,不在意让你的问题易于阅读和理解,那么我会认为你可能也不在意其他事情。感谢艾伦的回答。我已经将你的答案标记为可接受的解决方案。实际上,我最终使用了一个变量$refID和我拥有的每个部门参考资料,然后复制属性。给了我我想要的东西。@4kshay:太好了,它帮助了你!:)你也可以按箭头投票赞成答案:)谢谢