首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在查询的地图上应用周围的html元素

在查询的地图上应用周围的html元素
EN

Stack Overflow用户
提问于 2021-06-02 08:43:36
回答 1查看 30关注 0票数 1

我正在查询一个地图来构建一些元素,这些元素应该封装在元素html、head和body中。

我只是添加了'run‘键,因为我不知道如何调用第三个模板而不匹配地图中的某些内容。如果单独运行或同时运行,两个“存储”模板都会产生预期的结果,但是当试图将其包装到body元素中时(使用第三个模板),它将失败。

由于我计划模块化XSLT和模板,所以除非有必要,否则我不打算减少模板的数量。

JSON:

代码语言:javascript
复制
<data>
{

  "run": "",
  
  "store-1": {
    "pencils": 4,
    "rulers": 1
  },
  "store-2": {
    "milk": 2,
    "water": 5
  }
}
</data>

XSL:

代码语言:javascript
复制
<?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>

结果:

代码语言:javascript
复制
<?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>

通缉结果:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-02 08:57:46

我会在第一个模板中输出headbody,在第一个模板中创建html,然后添加第二个模板就可以使用其他模板:

代码语言:javascript
复制
  <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“容器是一个无键映射,因此,如果您想要匹配它,可以使用它。

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67801791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档