prosource

문서()와 함께 복사를 사용하여 XHTML 출력에 SVG 추가

probook 2023. 9. 15. 21:09
반응형

문서()와 함께 복사를 사용하여 XHTML 출력에 SVG 추가

XML을 처리하는 동안 참조된 SVG 파일을 복사하려고 합니다.href다음 행을 사용하여 내 출력 HTML에 직접 속성을 지정합니다.

 <xsl:copy-of copy-namespaces="yes" select="document(@href)"/>

copy-namespaces어차피 기본값은 "예"이기 때문에 꼭 필요한 것은 아니지만, 사용해 본 적이 있는지 여부에 대한 의문을 방지하기 위해 추가했습니다.

파일은 HTML로 복사되지만 네임스피드된 요소는 호스로 처리됩니다.예를 들어, 복사하기 전에 다음과 같이 보이는 파일:

  <rdf:RDF>
      <cc:Work rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
        <dc:title/>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g transform="translate(-519.21143,-667.79077)" id="layer1">
    <image xlink:href="data:image/png;base64

나중에는 이런 식으로 보입니다.

  <_0:RDF xmlns:_0="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <_0:Work xmlns:_0="http://creativecommons.org/ns#" about="">
        <_0:format xmlns:_0="http://purl.org/dc/elements/1.1/">image/svg+xml</_0:format>
        <_0:type xmlns:_0="http://purl.org/dc/elements/1.1/" resource="http://purl.org/dc/dcmitype/StillImage"/>
        <_0:title xmlns:_0="http://purl.org/dc/elements/1.1/"/>
      </_0:Work>
    </_0:RDF>
  </metadata>
  <g id="layer1" transform="translate(-519.21143,-667.79077)">
    <image href="data:image/png;base64

에 xlink 네임스페이스가 없습니다.href이미지 요소의 값이 특히 문제가 됩니다.

해석 없이 SVG 파일에서 읽을 수 있도록 어떻게 다르게 할 수 있는지에 대한 생각이 있습니까?

효과가 있는 솔루션을 한 가지 찾았습니다. 하지만 이 솔루션은 완벽한 솔루션이므로 보다 우아한 솔루션을 원합니다.

<xsl:template name="topic-image-svg">
    <!-- Generate tags to embed SWFs -->
    <xsl:element name="div">
      <xsl:if test="@width">
        <xsl:attribute name="width">
          <xsl:value-of select="@width"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@height">
        <xsl:attribute name="height">
          <xsl:value-of select="@height"/>
        </xsl:attribute>
      </xsl:if>     
        <xsl:apply-templates select="document(@href)" mode="svg"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*" mode="svg">
    <xsl:copy copy-namespaces="yes">
      <xsl:for-each select="@*">
        <xsl:choose>
          <xsl:when test="self::node()[name() = 'xlink:href']">
            <xsl:attribute name="xlink:href"><xsl:value-of select="."></xsl:value-of></xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy></xsl:copy>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
     <xsl:apply-templates mode="svg"></xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

이 XSLT 작업의 이유를 알게 된 것 같습니다.

http://www.w3schools.com/xsl/el_namespace-alias.asp

네임스페이스 변환이 완료되면 출력이 생성될 때까지 망친 네임스페이스는 그대로 유지됩니다.

언급URL : https://stackoverflow.com/questions/18541462/using-copy-of-with-document-to-add-svgs-to-xhtml-output

반응형