我想用值变量(Array)检查当前值。 不知何故,我需要在if测试中使用另一个循环。
我的目标是将sourceXml中的语言映射到testLanguages.xml
sourceXML
<?xml version="1.0" encoding="utf-8"?>
<a>
<b lang="en"></b>
<b lang="de"></b>
<b lang="it"></b>
<b lang="jp"></b>
</a>
testLanguages.xml
<?xml version="1.0" encoding="utf-8"?>
<languages>
<lang Key="de" Id="1031" />
<lang Key="en" Id="1033" />
</languages>
我当前的XSL无效:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*" />
<xsl:variable name="language" select="document('testLanguages.xml')/lang" />
<xsl:template match="/">
<xsl:template match="a">
<xsl:for-each select="./b">
<xsl:if test=".b[@lang=$language@Key]">
Only DE and EN should be here
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
解决方案XSLT 1.0 [没有任何<xsl:key>
- 用法]
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*" />
<xsl:variable name="language" select="document('testLanguages.xml')//lang" />
<xsl:template match="a">
<xsl:copy>
<xsl:for-each select="b[@lang = $language/@Key]">
<xsl:copy>
<xsl:copy-of select="@* | $language[@Key = current()/@lang]/@*"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b lang="en" Key="en" Id="1033"/>
<b lang="de" Key="de" Id="1031"/>
</a>
<强>摘要强>
你的一些模式与错误的孩子相匹配。输出可能与您的实际预期输出不同。