Json 在查询的地图上应用周围的html元素
我正在查询一个映射来构建一些元素,这些元素应该包装在元素html、head和body中 我刚刚添加了“run”键,因为我不知道如何调用第三个模板,而不匹配地图中的某些内容。如果两个“存储”模板单独运行或同时运行,则会产生预期的结果,但当尝试将它们包装到body元素中时(使用第三个模板),则会失败 由于我计划对XSLT和模板进行模块化,除非有必要,否则我不会减少模板的数量 JSON:Json 在查询的地图上应用周围的html元素,json,xml,xslt,xslt-3.0,Json,Xml,Xslt,Xslt 3.0,我正在查询一个映射来构建一些元素,这些元素应该包装在元素html、head和body中 我刚刚添加了“run”键,因为我不知道如何调用第三个模板,而不匹配地图中的某些内容。如果两个“存储”模板单独运行或同时运行,则会产生预期的结果,但当尝试将它们包装到body元素中时(使用第三个模板),则会失败 由于我计划对XSLT和模板进行模块化,除非有必要,否则我不会减少模板的数量 JSON: <data> { "run": "", &qu
<data>
{
"run": "",
"store-1": {
"pencils": 4,
"rulers": 1
},
"store-2": {
"milk": 2,
"water": 5
}
}
</data>
<?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>
{
“运行”:“,
“商店-1”:{
“铅笔”:4,
“统治者”:1
},
“商店2”:{
“牛奶”:2,
“水”:5
}
}
XSL:
<data>
{
"run": "",
"store-1": {
"pencils": 4,
"rulers": 1
},
"store-2": {
"milk": 2,
"water": 5
}
}
</data>
<?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>
办公室
{.}
{.}
我的头衔
结果:
<data>
{
"run": "",
"store-1": {
"pencils": 4,
"rulers": 1
},
"store-2": {
"milk": 2,
"water": 5
}
}
</data>
<?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>
我的头衔
4.
1.
2.
5.
想要的结果:
<data>
{
"run": "",
"store-1": {
"pencils": 4,
"rulers": 1
},
"store-2": {
"milk": 2,
"water": 5
}
}
</data>
<?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>
我的头衔
4.
1.
2.
5.
我会在第一个模板中输出头部
和身体
,在那里您创建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>
办公室
{.}
{.}
我的头衔