如何解决在查询的地图上应用周围的 html 元素
我正在查询地图以构建一些应该包含在元素 html、head 和 body 中的元素。
我刚刚添加了键“run”,因为我不知道如何在不匹配地图中的内容的情况下调用第三个模板。如果两个“store”模板单独运行或同时运行,它们都会产生预期的结果,但是当尝试将 then 包装在 body 元素中时,(使用第三个模板)它失败了。
由于我计划模块化 XSLT 和模板,除非必要,否则我不希望减少模板的数量。
JSON:
<data>
{
"run": "","store-1": {
"pencils": 4,"rulers": 1
},"store-2": {
"milk": 2,"water": 5
}
}
</data>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:item="http://www.example.org/1"
expand-text="yes"
>
<xsl:output method="xml" indent="yes"/>
<xsl:attribute-set name="base">
<xsl:attribute name="contextRef">office</xsl:attribute>
</xsl:attribute-set>
<!-- Block all data that has no user defined template -->
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<html>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</html>
</xsl:template>
<!-- Build elements in store [1] -->
<xsl:template name="items-store-1" match="*[@key = 'store-1']//*[@key and not(*)]">
<xsl:element
name="item:{@key}"
use-attribute-sets="base"
>{.}</xsl:element>
</xsl:template>
<!-- Build elements in store [2] -->
<xsl:template name="items-store-2" match="*[@key = 'store-2']//*[@key and not(*)]">
<xsl:element
name="item:{@key}"
use-attribute-sets="base"
>{.}</xsl:element>
</xsl:template>
<!-- Build surrounding elements -->
<xsl:template match="*[@key='run']">
<head><title>MyTitle</title></head>
<body>
<store-1>
<xsl:call-template name="items-store-1"/>
</store-1>
<store-2>
<xsl:call-template name="items-store-2"/>
</store-2>
</body>
</xsl:template>
</xsl:transform>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:item="http://www.example.org/1">
<head>
<title>MyTitle</title>
</head>
<body>
<store-1>
<item:run contextRef="office"/>
</store-1>
<store-2>
<item:run contextRef="office"/>
</store-2>
</body>
<item:pencils contextRef="office">4</item:pencils>
<item:rulers contextRef="office">1</item:rulers>
<item:milk contextRef="office">2</item:milk>
<item:water contextRef="office">5</item:water>
</html>
想要的结果:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:item="http://www.example.org/1">
<head>
<title>MyTitle</title>
</head>
<body>
<store-1>
<item:pencils contextRef="office">4</item:pencils>
<item:rulers contextRef="office">1</item:rulers>
</store-1>
<store-2>
<item:milk contextRef="office">2</item:milk>
<item:water contextRef="office">5</item:water>
</store-2>
</body>
</html>
解决方法
无论如何,我都会在您创建 head
的第一个模板中输出 body
和 html
,然后似乎添加第二个模板就足以使用您拥有的其他模板:>
<xsl:template match="data">
<html>
<head><title>MyTitle</title></head>
<body>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</body>
</html>
</xsl:template>
<xsl:template match="*:map[starts-with(@key,'store')]">
<xsl:element name="{@key}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
作为替代方案,您的 XML 转换 JSON 的“根”容器是一个无键映射,因此如果您想对其进行匹配,您可以使用
<xsl:transform version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:item="http://www.example.org/1"
expand-text="yes"
>
<xsl:output method="xml" indent="yes"/>
<xsl:attribute-set name="base">
<xsl:attribute name="contextRef">office</xsl:attribute>
</xsl:attribute-set>
<!-- Block all data that has no user defined template -->
<xsl:mode on-no-match="shallow-skip"/>
<!-- Parse JSON to XML -->
<xsl:template match="data">
<html>
<xsl:apply-templates select="json-to-xml(.)/*"/>
</html>
</xsl:template>
<xsl:template match="*:map[starts-with(@key,'store')]">
<xsl:element name="{@key}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- Build elements in store [1] -->
<xsl:template name="items-store-1" match="*[@key = 'store-1']//*[@key and not(*)]">
<xsl:element
name="item:{@key}"
use-attribute-sets="base"
>{.}</xsl:element>
</xsl:template>
<!-- Build elements in store [2] -->
<xsl:template name="items-store-2" match="*[@key = 'store-2']//*[@key and not(*)]">
<xsl:element
name="item:{@key}"
use-attribute-sets="base"
>{.}</xsl:element>
</xsl:template>
<xsl:template match="*:map[not(@key)]">
<head><title>MyTitle</title></head>
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
</xsl:transform>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。