RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-360485

Димаслав Кузнечков...'s questions

Martin Hope
Димаслав Кузнечков...
Asked: 2021-12-18 13:07:10 +0000 UTC

XSLT 中的 XPath 问题

  • 1

一个 XML 文件本质上由两部分组成:

  1. <posts> - 包含里面的帖子
  2. <users> - 里面包含有关用户的信息

xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="blog.xsl"?>

<blog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3.org blog.xsd">

  <posts>

    <post>
      <topic>Human Resource Machine</topic>
      <keyword>C#</keyword>
      <keyword>Game</keyword>
      <keyword>HRM</keyword>
      <date>2020-12-03</date>
      <message>Пишу тут клон игры Human Resource Machine для лабы по шарпу)))</message>
      <file src="images/hrm.gif" name="hrm"/>
      <comment user="1">
        <date>2020-12-03</date>
        <message>А на чем пишешь-то?</message>
        <comment user="0">
          <date>2020-12-03</date>
          <message>На винформах)))</message>
          <comment user="1">
            <date>2020-12-03</date>
            <message>Ты псих...</message>
            <comment user="0">
              <date>2020-12-03</date>
              <message>Я знаю...</message>
            </comment>
          </comment>
        </comment>
      </comment>
    </post>

  </posts>

  <users>

    <user id="0" admin="true">
      <name>A.L.E.X.</name>
      <phone>+380990315102</phone>
      <mail>akrutko472@gmail.com</mail>
    </user>

    <user id="1" admin="false">
      <name>XXX</name>
      <phone>+88005553535</phone>
      <mail>xxx@gmail.com</mail>
    </user>

  </users>

</blog>

通过 xsd 获得更多详细信息:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

  <xs:simpleType name="nameType">
    <xs:restriction base="xs:string">
      <xs:minLength value="1"></xs:minLength>
      <xs:maxLength value="25"></xs:maxLength>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name = "phoneType">
    <xs:restriction base="xs:string">
      <xs:pattern value="^\+? ?\d([ -]?\d){9,14}$"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name = "mailType">
    <xs:restriction base="xs:string">
      <xs:pattern value="^\w[\.\w]*@[\w]+\.[A-Za-z]+$"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="messageType">
    <xs:restriction base="xs:string">
      <xs:maxLength value="1000"></xs:maxLength>
    </xs:restriction>
  </xs:simpleType>

  <xs:complexType name="fileType">
    <xs:attribute name="src" type="xs:anyURI" use="required"/>
    <xs:attribute name="name" type="nameType" use="optional" />
  </xs:complexType>

  <xs:complexType name="postType">
    <xs:sequence>
      <xs:element name="topic" type="nameType"></xs:element>
      <xs:element name="keyword" type="nameType" minOccurs="0" maxOccurs="unbounded"></xs:element>
      <xs:element name="date" type="xs:date"></xs:element>
      <xs:element name="message" type="messageType"></xs:element>
      <xs:element name="file" type="fileType" minOccurs="0" maxOccurs="10"></xs:element>
      <xs:element name="comment" type="commentType" minOccurs="0" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="commentType">
    <xs:sequence>
      <xs:element name="date" type="xs:date"></xs:element>
      <xs:element name="message" type="messageType"></xs:element>
      <xs:element name="file" type="fileType" minOccurs="0" maxOccurs="3"></xs:element>
      <xs:element name="comment" type="commentType" minOccurs="0" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
    <xs:attribute name="user" type="xs:nonNegativeInteger" use="required"/>
  </xs:complexType>

  <xs:complexType name="userType">
    <xs:sequence>
      <xs:element name="name" type="nameType"/>
      <xs:element name="phone" type="phoneType"/>
      <xs:element name="mail" type="mailType"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:nonNegativeInteger" use="required"/>
    <xs:attribute name="admin" type="xs:boolean" default="false"/>
  </xs:complexType>

  <xs:complexType name="postsType">
    <xs:sequence>
      <xs:element name = "post" type="postType" minOccurs="0" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name ="usersType">
    <xs:sequence>
      <xs:element name = "user" type="userType" minOccurs="0" maxOccurs="unbounded"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="blogType">
    <xs:sequence>
      <xs:element name="posts" type="postsType"></xs:element>
      <xs:element name="users" type="usersType"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name ="blog" type="blogType"></xs:element>

</xs:schema>

在 XSLT 中,我想直接在评论中呈现用户的昵称。而此时,困难就来了(这一行在特定的<comment>范围内被调用):

<xsl:value-of select="/blog/users/user[@id = '{@user}']/name"/>

理论上,一切都是正确的,但最终什么也没有发生。在这种情况下,特殊情况起作用,例如:

<xsl:value-of select="/blog/users/user[@id = '1']/name"/>

我还在关卡上渲染了一些东西:

<p class="/blog/users/user[@id = '{@user}']/name"/>

在呈现的页面中,<p> 类实际上是正确的,即 XPath 本身是正确编写的,但由于某种原因,XSLT 不希望在 <xsl:value-of> 内正确处理路径。唯一的建议是为此还有另一个 xsl - 我什至不怀疑存在的指令。您也可能需要为此使用变量,我尝试了很多 - 最后什么也没发生。还是问题的本质是别的?

最被截断的 xsl:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!--Рекурсивно вызываемый шаблон комментария-->
  <xsl:template name="comment">
    
    <div class="comment">
      
      <p class="author">
        <xsl:value-of select="/blog/users/user[@id = '1']/name"/> <!--Конкретный пользователь - работает-->
        <p class="/blog/users/user[@id = '{@user}']/name"/> <!--XPath как класс <p> - значение корректно-->
        <xsl:value-of select="/blog/users/user[@id = '{@user}']/name"/> <!--Уже не работает...-->
      </p>
      
      <p><xsl:value-of select="message"/></p>
      <div class="info"><xsl:value-of select="date"/></div>

      <xsl:if test="comment">
        <hr/>
        <details>
          <summary>Ответы</summary>
          <xsl:for-each select="comment">
            <xsl:call-template name="comment"/>
          </xsl:for-each>
        </details>
      </xsl:if>
      
    </div>
  
  </xsl:template>

  <xsl:template match="/">

  <html>

    <head>
        <meta charset = "UTF-8"/>
        <title>Блог</title>
        <link rel = "stylesheet" href = "styles/main.css"/>
        <script src="scripts/main.js" defer="true"></script>
    </head>

    <body>

      <xsl:for-each select="blog/posts/post">

        <div class="post">

          <div class="topic"><xsl:value-of select="topic"/></div>

          <div class = "keywords">
            <xsl:for-each select="keyword">
              <span><xsl:value-of select="."/></span>
            </xsl:for-each>
          </div>
          
          <p><xsl:value-of select="message"/></p>
          <div class="info"><xsl:value-of select="date"/></div>

          <xsl:if test="comment">
            <hr/>
            <details>
              <summary>Комментарии</summary>
              <xsl:for-each select="comment">
                <xsl:call-template name="comment"/>
              </xsl:for-each>
            </details>
          </xsl:if>
        
        </div>

      </xsl:for-each>

    </body>

  </html>
  
  </xsl:template>

</xsl:stylesheet>

预期结果:

在页面上显示帖子

这是一个不提供后端的实验室作业,因此不使用 xslt 处理器。换句话说,xslt 处理器内置在浏览器中。

xml
  • 2 个回答
  • 10 Views
Martin Hope
Димаслав Кузнечков...
Asked: 2020-11-22 01:59:05 +0000 UTC

为什么在实践中移动语义比复制语义慢?

  • 2

晚上在小屋里,亲爱的程序员。

我阅读了一篇关于移动语义、对 r-value 和 std::move() 的引用的文章,之后我很高兴地检查了通过快速而懒惰地编写两个冒泡排序实现可以获得多少时间:

#include <iostream>
#include <utility>
#include <ctime>
#include <cstdlib>

template <typename T>
bool check(T* arr, int n)
{
    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i] > arr[i + 1]) return 0;
    };
    return 1;
}

template <typename T>
void bubblesortcopy(T* arr, int n)
{
    while (true)
    {
        bool op = false;
        for (int j = 0; j < n - 1; j++)
        {
            if (arr[j + 1] < arr[j])
            {
                T h = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = h;
                op = true;
            };
        };
        if (!op) return;
        n--;
    };
};

template <typename T>
void bubblesortmove(T* arr, int n)
{
    while (true)
    {
        bool op = false;
        for (int j = 0; j < n - 1; j++)
        {
            if (arr[j + 1] < arr[j])
            {
                T h { std::move(arr[j + 1]) };
                arr[j + 1] = std::move(arr[j]);
                arr[j] = std::move(h);
                op = true;
            };
        };
        if (!op) return;
        n--;
    };
};

int main()
{
    int n;
    clock_t start;
    clock_t end;
    std::cin >> n;
    int* arr1 = new int[n];
    int* arr2 = new int[n];
    for (int i = 0; i < n; i++)
    {
        arr1[i] = rand();
        arr2[i] = arr1[i];
    };
    start = clock();
    bubblesortcopy(arr1, n);
    end = clock();
    if(check(arr1, n)) std::cout << difftime(end, start) / CLOCKS_PER_SEC << std::endl;
    start = clock();
    bubblesortmove(arr2, n);
    end = clock();
    if(check(arr2, n)) std::cout << difftime(end, start) / CLOCKS_PER_SEC << std::endl;
    delete[] arr1;
    delete[] arr2;
};

结果,期望和现实大相径庭:

...

向菜鸟解释什么是错的?或者它是一个特性而不是一个错误?

c++
  • 2 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5