我正在查询一个地图来构建一些元素,这些元素应该封装在元素html、head和body中。
我只是添加了'run‘键,因为我不知道如何调用第三个模板而不匹配地图中的某些内容。如果单独运行或同时运行,两个“存储”模板都会产生预期的结果,但是当试图将其包装到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>发布于 2021-06-02 08:57:46
我会在第一个模板中输出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的"root“容器是一个无键映射,因此,如果您想要匹配它,可以使用它。
<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>https://stackoverflow.com/questions/67801791
复制相似问题